std::vector 重载的模板化方法未生效
templated method with std::vector overload not taking hold
我知道这个问题已经被问过很多次了,但是参考这个问题的答案:
How can I specialize a template member function for std::vector<T>
如果我使用 GCC9 尝试一个简单的例子,它根本不起作用,我不明白为什么不行。我在这里遗漏了什么吗?
#include <vector>
#include <stdio.h>
template<typename T>
std::vector<T> foo( const std::vector<T> & )
{
printf( "vector!\n" );
return std::vector<T>();
}
template<typename T>
T foo( const T & )
{
printf( "non vector!\n" );
return T();
}
int main()
{
foo<int>( *( int * ) nullptr);
foo<std::vector<int>>( *( std::vector<int> * ) nullptr);
}
输出:
non vector!
non vector!
尝试更改:
foo<std::vector<int>>( *( std::vector<int> * ) nullptr);
至
foo<int>( *( std::vector<int> * ) nullptr);
您的顶级模板需要类型名称 T,并且该方法采用 T 的向量。
我知道这个问题已经被问过很多次了,但是参考这个问题的答案: How can I specialize a template member function for std::vector<T>
如果我使用 GCC9 尝试一个简单的例子,它根本不起作用,我不明白为什么不行。我在这里遗漏了什么吗?
#include <vector>
#include <stdio.h>
template<typename T>
std::vector<T> foo( const std::vector<T> & )
{
printf( "vector!\n" );
return std::vector<T>();
}
template<typename T>
T foo( const T & )
{
printf( "non vector!\n" );
return T();
}
int main()
{
foo<int>( *( int * ) nullptr);
foo<std::vector<int>>( *( std::vector<int> * ) nullptr);
}
输出:
non vector!
non vector!
尝试更改:
foo<std::vector<int>>( *( std::vector<int> * ) nullptr);
至
foo<int>( *( std::vector<int> * ) nullptr);
您的顶级模板需要类型名称 T,并且该方法采用 T 的向量。