类与函数模板专业化



我最近才从这里学到C++中的部分模板专门化,它完美地解决了一个问题,即我需要一个模板类来区分指针和非指针。一个简化的例子是:

// main.cpp
template <typename T>
class C
{
public:
C( const T& t ) : t_( t ) {}
private:
T t_;
};
template <typename T>
class C<T*>
{
public:
C( const T& t ) : t_( new T(t) ) {}
~C() { delete t_; }
private:
T* t_;
};
int main( int argc, char* argv[] )
{
C<int>  c1(4);
C<int*> c2(2);
return 0;
}

我想尝试将这个想法扩展到函数中,但遇到了我不理解的编译器错误:

// main.cpp
#include <set>
template <typename T>
std::set<T> makeSet( size_t off )
{
std::set<T> s;
s.insert( T() + T(off) );
return s;
}
template <typename T>
std::set<T*> makeSet<T*>( size_t off )
{
std::set<T*> s;
T* t = new T( T() + T(off) );
s.insert( t );
return s;
}
int main( int argc, char* argv[] )
{
std::set<int>  s1 = makeSet<int>(4);
std::set<int*> s2 = makeSet<int*>(2);
delete *(s2.begin());
return 0;
}

$ g++ --version && g++ -g ./main.cpp
g++ (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
./main.cpp:13:38: error: non-class, non-variable partial specialization ‘makeSet<T*>’ is not allowed
13 | std::set<T*> makeSet<T*>( size_t off )
|                                      ^
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:23:37: error: call of overloaded ‘makeSet<int>(int)’ is ambiguous
23 |   std::set<int>  s1 = makeSet<int>(4);
|                                     ^
./main.cpp:5:13: note: candidate: ‘std::set<T> makeSet(size_t) [with T = int; size_t = long unsigned int]’
5 | std::set<T> makeSet( size_t off )
|             ^~~~~~~
./main.cpp:13:14: note: candidate: ‘std::set<T*> makeSet(size_t) [with T = int; size_t = long unsigned int]’
13 | std::set<T*> makeSet<T*>( size_t off )
|              ^~~~~~~~~~~
./main.cpp:24:38: error: call of overloaded ‘makeSet<int*>(int)’ is ambiguous
24 |   std::set<int*> s2 = makeSet<int*>(2);
|                                      ^
./main.cpp:5:13: note: candidate: ‘std::set<T> makeSet(size_t) [with T = int*; size_t = long unsigned int]’
5 | std::set<T> makeSet( size_t off )
|             ^~~~~~~
./main.cpp:13:14: note: candidate: ‘std::set<T*> makeSet(size_t) [with T = int*; size_t = long unsigned int]’
13 | std::set<T*> makeSet<T*>( size_t off )
|              ^~~~~~~~~~~

我对此有点怀疑,因为这看起来像是试图用不同的返回类型但具有相同参数的函数重载——我知道这在其他方面是非法的,但我认为因为这些都是模板函数,所以在调用函数时指定模板类型(如上面的main()(可以消除歧义。

但我不确定编译器在第13行抱怨的是什么:non-class, non-variable partial specialization ‘makeSet<T*>’ is not allowed错误是什么意思

我想做的事情在C++中是否可行?也就是说,是否可以制作一个模板函数,使其对指针和非指针模板类型表现不同,并返回指定类型的STL容器(C++98、03、11和14之间的答案会不同吗?(

non-class, non-variable partial specialization ‘makeSet<T*>’ is not allowed错误是什么意思?

让我们看看cppreference.com上关于部分模板专业化的一些文档:

允许为给定类别的模板参数自定义类[和变量(自C++14以来(]模板。

类和变量模板允许进行部分专门化。编译器告诉您,您的模板既不适用于变量的类。因此,不允许部分专业化。

编译器消息的一个不那么令人困惑的措辞是:函数模板的部分专用化是不允许的。


至于可以做什么,可以用合适的operator()声明一个类模板。在某些情况下,这与使用函数模板差不多。

你可以尝试这样的东西(C++17(:

template <typename T>
std::set<T> makeSet( size_t off )
{
std::set<T> s;
if constexpr(std::is_pointer_v<T>)
{
using U = std::remove_reference_t<decltype(*std::declval<T>())>;
s.insert(new U( U() + U(off) ));
}
else
{
s.insert( T() + T(off) );
}
return s;
}

最新更新