Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::string not a valid type for a template constant parameter?

Tags:

c++

templates

I'm trying to create a class where a user can store different type of data in maps. I've created a map for bool, int and std::string and created template functions so that I don't have to rewrite the get and set functions for each type.

Here's a minmal version of my code:

#include <map>
#include <string>
#include <stdexcept>
#include <iostream>

class Options {
public:
    template<class T>
    void Set(const std::string& name, const T& value) {
        GetMap<T>()[name] = value;
    }
    template<class T>
    T Get(const std::string& name) {
        auto it = GetMap<T>().find(name);
        if(it == GetMap<T>().end()) {
            throw std::runtime_error(name + " not found");
        }
        return it->second;
    }
private:
    std::map<std::string, int> ints_;
    std::map<std::string, std::string> strings_;
    std::map<std::string, bool> bools_;

    template<class T>
    std::map<std::string, T>& GetMap();
    template<bool>
    std::map<std::string, bool>& GetMap() {
        return bools_;
    }
    template<std::string> // error
    std::map<std::string, std::string>& GetMap() {
        return strings_;
    }
    template<int>
    std::map<std::string, int>& GetMap() {
        return ints_;
    }
};

int main() {
    Options o;
    o.Set("test", 1234);
    o.Set<std::string>("test2", "Hello World!");
    std::cout << o.Get<int>("test") << std::endl
              << o.Get<std::string>("test2") << std::endl;
}

I'm getting the following error:

error: 'struct std::basic_string<char>' is not a valid type for a template constant parameter

But why?

like image 476
jhasse Avatar asked Mar 03 '26 04:03

jhasse


2 Answers

If I understand correctly, you're trying to specialise the function template GetMap(). But your syntax is incorrect; you probably want:

template<class T>
std::map<std::string, T>& GetMap();

template<>
std::map<std::string, bool>& GetMap<bool>() {
    return bools_;
}

and so on.

like image 66
Oliver Charlesworth Avatar answered Mar 04 '26 16:03

Oliver Charlesworth


Two points:

  • The specialization should be outside the class (important), otherwise it will not compile
  • The correct syntax of the specialization is as follows:

    //outside the class definition
    
    template<>
    std::map<std::string, bool>& Options::GetMap<bool>() {
                               //^^^^^^^^^ dont forget this!
          return bools_;
    }
    template<>
    std::map<std::string, std::string>& Options::GetMap<std::string>() {
                                       //^^^^^^^^^ dont forget this!
          return strings_;
    }
    template<>
    std::map<std::string, int>& Options::GetMap<int>() {
                               //^^^^^^^^^ dont forget this!
          return ints_;
    }
    
like image 41
Nawaz Avatar answered Mar 04 '26 18:03

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!