简体   繁体   中英

C++ better way to emplace char* strings into a std::vector<string>?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM