Search code examples
c++templatesc++11auto

Using auto as a template parameter


I'm attempting to compile the following using GCC 4.7.1 with the -std=c++11 flag set:

std::map<std::string, auto> myMap;

I'm attempting to create an object to contain a large amount of Json data of various types (int string, bool) as well as sub-structures (list, map) so I can't declare the type of the field value at compile time, so I thought I'd use the auto keyword for it.

However, when I try to compile it, I get the following

error: invalid use of ‘auto’
error: template argument 2 is invalid
error: template argument 4 is invalid
error: unable to deduce ‘auto’ from ‘<expression error>’

Is there a special way to use auto as a template argument, or is it just not possible?


Solution

  • I think what you are looking for is boost::any.

    std::map<std::string, boost::any> myMap;
    

    auto is evaluated during compile time and cannot be used as a dynamic run-time type.