Template function overload for type containing a type

妖精的绣舞 提交于 2019-12-22 05:33:08

问题


I'm trying to do the following:

#include <iostream>
#include <vector>
#include <tuple>
#include <list>

template <typename T>
void f(T t) {
    std::cout << "1" << std::endl;
}

template <typename T, typename V>
void f(T<std::tuple<V>> t) {
    std::cout << "2" << std::endl;
}

int main() {
    f(std::list<double>{}); // should use first template
    f(std::vector<std::tuple<int>>{}); // should use second template
}

What is the simplest way to do this in C++14? I thought that I could sort of pattern match in this way but the compiler won't have it.


回答1:


The template parameter T is used as a template-name, so it should be declared as template template parameter. e.g.

template <template <typename...> class T, typename V>
//        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void f(T<std::tuple<V>> t) {
    std::cout << "2" << std::endl;
}

LIVE



来源:https://stackoverflow.com/questions/44511121/template-function-overload-for-type-containing-a-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!