Home:ALL Converter>Template template parameters without specifying inner type

Template template parameters without specifying inner type

Ask Time:2016-07-05T18:22:10         Author:Konrad

Json Formatter

I want to know if it is at all possible to have code that has the following behaviour:

int main()
{
    func<vector>(/*some arguments*/);
}

That is, I want the user to be able to specify the container without specifying the type that it operates on.

For example, some (meta) code (which does not work with the above) that might define func would be as follows:

template<typename ContainerType>
int func(/*some parameters*/)
{
    ContainerType<int> some_container;
    /*
        operate on some_container here
    */
    return find_value(some_container);
}

Author:Konrad,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38200959/template-template-parameters-without-specifying-inner-type
Jarod42 :

The syntax is\n\ntemplate <template <typename...> class ContainerType>\nint func(/*some parameters*/)\n{\n // Your code with ContainerType<int>\n}\n\n\nNote: class cannot be replaced by typename (until c++17).\n\nYou cannot simply use typename instead of typename... Because std::vector\n takes Type and Allocator (defaulted): std::vector<T, Allocator>",
2016-07-05T10:25:20
yy