简体   繁体   中英

How to template a function with a mix of non-type template parameters and type template parameters?

Minimal Reproducible Example

#include <unordered_map>
#include <string>

template<class T, bool did_work>
class Test {
    Test(T input) : field(input), success(did_work) {}
    T field;
    bool success;
};

template<typename A>
void func(std::unordered_map<std::string, Test<A, bool>> input1, A input2) {}

int main() {}

Output:

$ g++ -std=c++17 -ggdb -g3 -Wall test.cpp && ./a.out 
test.cpp:12:51: error: type/value mismatch at argument 2 in template parameter list for ‘template<class T, bool did_work> class Test’
   12 | void func(std::unordered_map<std::string, Test<A, bool>> input1, A input2) {}
      |                                                   ^~~~
test.cpp:12:51: note:   expected a constant of type ‘bool’, got ‘bool’
test.cpp:12:55: error: template argument 2 is invalid
   12 | void func(std::unordered_map<std::string, Test<A, bool>> input1, A input2) {}
      |                                                       ^~
test.cpp:12:55: error: template argument 5 is invalid

I need to be able to pass in unordered_map<std::string, Test<A, true>> as well as unordered_map<std::string, Test<A, false>> . Is there a way to do this in the function definition without changing the class Test definition?

With template<class T, bool did_work> did_work is a non-type template parameter. What that means is that instead of passing a type to it, it takes a value. Since it needs a value, you make a Test like Test<A, true> or Test<A, false> , not Test<A, bool> .

For your function you can just add a non-type template parameter to do this for you like

template<typename A, bool did_work>
void func(std::unordered_map<std::string, Test<A, did_work>> input1, A input2) {}

Do it like this:

#include <unordered_map>
#include <string>

template<class T, bool did_work>
class Test {
    Test(T input) : field(input), success(did_work) {}
    T field;
    bool success;
};

template<typename A, bool b = true>
void func(std::unordered_map<std::string, Test<A, b>> input1, A input2)
{

}

int main() {

}

Update

I mean that you write something like:

#include <unordered_map>
#include <string>
#include <iostream>

template<class T, bool did_work>
class Test {
public:
    Test(T input) : field(input), success(did_work) {}
private:
    T field;
    bool success;
};

template<
    typename UnorderedMap
    ,typename A
> requires requires (UnorderedMap map){
    map.size();
}
void func(
    UnorderedMap input1,
    A input2)
{
    std::cout << input1.size();
}

int main() 
{
    using A = int;
    Test<A, true> t{12};
    std::unordered_map<std::string, Test<A, true>> map{};
    func(map, 12);

    //this would fail!
    //func(12, 12);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM