I'm trying to aggregate the value of member variables for some arbitrary number of a specific type hierarchy that I have defined.
This is fine and easy to do using a variadic templated function.
Where I am not having much luck is when I try to pass in the name of the function that is calling this template as a string as the first argument. This is rather hard to explain so below is some code that I have to demonstrate what I am trying to do.
Trying to compile this results in: error C2993: 'std::string' : illegal type for non-type template parameter 'str'
Is what I am attempting to do impossible? Or am I just going about it this in the wrong way?
std::ostringstream output;
int objCounter = 0;
void set_results() {
output << "\n" << std::endl;
}
template <typename obj, typename ...objs>
void set_results(obj objHead, objs... objTail) {
++objCounter;
output << "Argument (" << objCounter << ") \t" << objHead.bytes << std::endl;
set_results(objTail...);
}
template <std::string str, typename ...objs>
void set_results(std::string objHead, objs... objTail) {
output << "Leaf Function: " << objHead << std::endl;
set_results(objTail...);
}
class objDep {
public:
int bytes;
objDep(int b) : bytes(b) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
objDep one(1);
objDep two(2);
objDep three(3);
objDep four(4);
set_results(std::string("main"), one, two, three, four);
std::cout << output.str() << std::endl;
return 0;
}
Change
template <std::string str, typename ...objs>
void set_results(std::string objHead, objs... objTail) {
output << "Leaf Function: " << objHead << std::endl;
set_results(objTail...);
}
to:
template <typename ...objs>
void set_results(std::string objHead, objs... objTail) {
output << "Leaf Function: " << objHead << std::endl;
set_results(objTail...);
}
You can't have a template parameter of type std::string
, and at any rate it's entirely unclear what you intended for str
to represent anyway.
Note that function templates cannot be partially specialized. With this replacement, you'll have two overloads of set_results
. The one with the std::string
parameter will be selected because it's more specialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With