Force a specific overload when template template

早过忘川 提交于 2019-12-23 08:53:08

问题


Consider the following code :

#include <iostream>
#include <vector>
#include <type_traits>

// Version A
template<typename T>
void f(const T& x)
{
    std::cout<<"Version A"<<std::endl;
}

// Version B
template<template<typename> class T, typename T1>
void f(const T<T1>& x)
{
    std::cout<<"Version B"<<std::endl;
}

// Main
int main(int argc, char* argv[])
{
    f(double());
    f(std::vector<double>()); // <- How to force the use of version B ?
    return 0;
}

By default, it will produce :

Version A
Version A

How to force the use of Version B when the passed type is a template template with the good shape (I can add new versions of f, I can add std::enable_if or other C++11 type traits syntax, but if possible I would like to avoid adding an helper class) ?


回答1:


std::vector does not take a single typename parameter, it takes 2! Don't forget the allocator.

Thus, use variadic templates:

template<template<typename...> class T, typename T1>
void f(const T<T1>& x)
{
    std::cout<<"Version B"<<std::endl;
}

Now it works as you want.




回答2:


As Pubby explained in his answer, std::vector is a template with two parameters, therefore your overload function needs to take more template parameters. If you do not want to use variadic templates, then you need to set correct number of parameters :

#include <iostream>
#include <vector>
#include <type_traits>


// Version A
template<typename T>
void f(const T& )
{
    std::cout<<"Version A"<<std::endl;
}

// Version B
template<template<typename,typename> class T, typename T1,typename T2>
void f(const T<T1,T2>& )
{
    std::cout<<"Version B"<<std::endl;
}

template<typename T>
void baa(const T&)
{
}

// Main
int main()
{
    f( double() );
    f( std::vector<double>() ); // <- How to force the use of version B ?
}


来源:https://stackoverflow.com/questions/13910355/force-a-specific-overload-when-template-template

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