12

I have several std::unordered_maps. They all have an std::string as their key and their data differs. I want to make a csv string from a given map's keys because that data needs to be sent over the wire to a connected client. At the moment I have a method for each individual map. I wanted to make this generic and I came up with the following :

std::string myClass::getCollection(auto& myMap) {
    std::vector <std::string> tmpVec;
    for ( auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}

I compile with gcc 6.1.0 and -std=c++14 using eclipse and it compiles but it doesn't link. The linker complains about undefined reference to std::__cxx11::getCollection(someMap);

Regardless of the map data and the way I call it, it always tells me :

Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> getCollection() '

How do I solve this?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
ZoOl007
  • 407
  • 3
  • 22
  • 5
    `std::string myClass::getCollection(auto& myMap)` is not valid syntax. Specifically, `auto` is not a valid parameter type for a member function. – ildjarn Oct 07 '16 at 10:54
  • is what I'm trying to accomplish possible using another approach then? I thought it was possible to use auto as a parameter in c++14... apparently I'm wrong then... – ZoOl007 Oct 07 '16 at 10:59
  • 4
    "*I thought it was possible to use auto as a parameter in c++14...*" Only for lambdas. "*is what I'm trying to accomplish possible using another approach then?*" Yes, just use normal templates: `template std::string myClass::getCollection(MapT& myMap)` – ildjarn Oct 07 '16 at 11:01
  • 9
    Using `auto` in function parameters is currently non-standard, but might get into the language in C++20. It's called an "abbreviated function template" and I think it's part of the Concepts proposal. GCC provides it as an extension currently; if you compile with `-pedantic` it'll fail. – TartanLlama Oct 07 '16 at 11:04
  • 1
    Since 'auto' parameter is just like a template param, you should define (not just declare) the member function in the header file - otherwise the definition won't get instantiated in some translation units. – Igor R. Oct 07 '16 at 11:12
  • 2
    Missing a [mcve] for the link error... – Marc Glisse Oct 07 '16 at 11:23
  • @Marc Glisse - I'll make one and add it to the question – ZoOl007 Oct 07 '16 at 11:30

3 Answers3

15

As in C++14 auto parameters are only allowed in lambdas (as per @ildjarn's comment), you can just develop a function template, templateized on the map type, e.g.:

#include <sstream>
#include <string>
#include <vector>

class myClass {
...

template <typename MapType>
std::string getCollection(const MapType& myMap) {
    std::vector <std::string> tmpVec;
    for ( const auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( const auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}

Note also the addition of const for some const-correctness.

Moreover, why not just building the output string directly using the string stream object, without populating an intermediate vector<string> (which is more code, more potential for bugs, more overhead, less efficiency)?

And, since you are just interested in using the string stream as an output stream, using ostringstream instead of stringstream is better as it's more efficient and communicates your intent better.

#include <sstream>  // for std::ostringstream
#include <string>   // for std::string
...

template <typename MapType>
std::string getCollection(const MapType& myMap) {
    std::ostringstream ss;
    for (const auto& elem : myMap) {
        ss << elem.first << ',';
    }
    std::string result = ss.str();
    result.pop_back(); // remove the last ','
    return result;
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Thank you for your improvements. I keep having the same linking problems... undefined reference to `std::__cxx11:: – ZoOl007 Oct 07 '16 at 11:18
  • Do you have the template method implemented inline in some header, or at least accessible by your client's code? Do you include all the required headers (e.g. ``, ``, ``, your map collection's headers)? – Mr.C64 Oct 07 '16 at 11:21
  • yes, I have template std::string getCollection(MapType& myMap); in my header file that is included in the cpp where the template/function is and also included in another header where the class's method gets called – ZoOl007 Oct 07 '16 at 11:24
  • 1
    @ZoOl007: The function template must be *implemented* inline in a header file if you want to call it in another header or .cpp file. It's not like for ordinary non-templated functions, that you can declare in a header and implement in a separate .cpp file. – Mr.C64 Oct 07 '16 at 11:32
  • ok, it works now... this is new for me... so, all templated functions are best put completely in a header file then? Confusing difference. Thank you very much for your time! – ZoOl007 Oct 07 '16 at 11:45
7

Why not just use a template?

template <typename TMap>
std::string myClass::GetCollection(TMap &myMap) {
    std::vector <std::string> tmpVec;
    for ( auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}

Your method is exactly the same, but instead of the auto keyword, we use template function syntax to handle the type inference.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
6

auto parameters are only allowed in lambdas in C++14.

Probably this is since in an classic function like yours you could have declared a function template (which is basically what happens in the lambda case) while lambdas can't be templates.

Motti
  • 110,860
  • 49
  • 189
  • 262