-1

I am populating a string vector from any of char[] char* std::string by emplacing them in the std::vector

This code works but is a bit clunky looking and takes three templates to cover variadics and initializer lists.

Is there a more canonical idiom for this kind of thing?

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <stdexcept>


// safe function for putting char* strings into a std::vector<std::string>
template <typename T, typename C>
std::vector<T> & safe_emplace( std::vector<T>& vec, C& c)
{
  if ( !c )  return  vec;

  vec.emplace_back(c);

  return vec;

}


// variadic version
template <typename T, typename C, typename...  Args>
std::vector<T> & safe_emplace( std::vector<T>& vec, C& c, Args... args)
{

  safe_emplace(vec, c); 

  return safe_emplace( vec, args...);

}


// initializer list version
template <typename T>
std::vector<T> & safe_emplace( std::vector<T>& vec, const std::initializer_list<std::string> il)
{
  for (auto& s: il)
    vec.emplace_back(s);

  return vec;
}





int main( int argc, char* argv[])
{
  std::vector<std::string> svec;

  char one[] = "string one";

  char two[] = "string two";

  char three[] = "string three";

  char* d = new char[10];

  char* n = NULL;

  std::strncpy(d, "0123456789\0", 10);

  safe_emplace(svec, one);   // char[]

  safe_emplace(svec, two, three); // variadic

  safe_emplace(svec, d);  // char* 

  safe_emplace(svec, n); // char* NULL

  safe_emplace(svec,   "five", "four", "three", "two", "one", nullptr);

  safe_emplace(svec,   {"A", "B", "C", "D", "E"} );



  for (auto s : svec)
    std::cout << s << "\n";

  delete[] d; // clean up d (new)


  return 0;
}

I specifically wanted to handle the case of a NULL char* which I decided to skip it instead of create an empty string.

I had a try/catch for nullptr, but found it was not necessary with the templates.

Chris Reid
  • 460
  • 4
  • 9
  • 3
    Questions about code that works but you'd like to work better should be asked at [Code Review](https://codereview.stackexchange.com/help/asking). Yes, I linked to the how to ask page. If you aren't familiar with Code Review, that's the best place to start. – user4581301 Jan 22 '19 at 00:12
  • 3
    `d` is not zero-terminated, so `safe_emplace(svec, d);` is UB. – Sid S Jan 22 '19 at 00:14
  • 2
    "*specifically wanted to handle the case of a NULL `char*`*" - then I suggest providing an overload of `safe_emplace()` just for `char*`. – Remy Lebeau Jan 22 '19 at 00:31

1 Answers1

1

This code works but is a bit clunky looking and takes three templates to cover variadics and initializer lists.

Is there a more canonical idiom for this kind of thing?

Not really, since you have to handle the variadic template and initializer_list separately.

I specifically wanted to handle the case of a NULL char* which I decided to skip it instead of create an empty string.

Then you should provide an overload of safe_emplace() to handle char* data separately from other types.

Try something more like this instead:

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

// rename the actual emplacement functions to avoid unwanted
// recursive loops in the variadic template iteration...

template <typename Container>
Container& do_safe_emplace(Container &c, const char *value)
{
    if (value) c.emplace_back(value);
    return c;
}

template <typename Container>
Container& do_safe_emplace(Container &c, const typename Container::value_type &value)
{
    c.emplace_back(value);
    return c;
}

// this overload is needed to handle when 'args...' becomes blank
// at the end of the variadic template loop iteration...
template <typename Container>
Container& safe_emplace(Container &c)
{
    return c;
}

template <typename Container, typename T, typename... Args>
Container& safe_emplace(Container &c, const T &value, Args... args)
{
    do_safe_emplace(c, value); 
    return safe_emplace(c, args...);
}

template <typename Container, typename T>
Container& safe_emplace(Container &c, const std::initializer_list<T> il)
{
    for (auto& value: il)
        do_safe_emplace(c, value);
    return c;
}

int main()
{
    std::vector<std::string> svec;

    char one[] = "string one";
    char two[] = "string two";
    char three[] = "string three";
    std::string four = "string four";
    char* d = new char[11]; // <- need room for null terminator
    char* n = NULL;

    std::strncpy(d, "0123456789", 11);

    safe_emplace(svec, one);   // char[]
    safe_emplace(svec, two, three, four); // variadic
    safe_emplace(svec, d);  // char* 
    safe_emplace(svec, n); // char* NULL
    safe_emplace(svec, "five", "four", std::string("three"), "two", "one", nullptr);
    safe_emplace(svec, {"A", "B", "C", "D", "E"} );

    for (auto &s : svec)
        std::cout << s << "\n";

    delete[] d; // clean up d (new)

    return 0;
}

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770