我正试图使用C++概念来编写一个类型特征,该特征将根据其模板参数是否为基本类型而产生不同的类型:
template<typename T>
concept fundamental = std::is_fundamental_v<T>;
template<typename T>
concept non_fundamental = !std::is_fundamental_v<T>;
以下代码与预期一样工作:
void Print(fundamental auto value)
{
std::cout << "fundamentaln";
}
void Print(non_fundamental auto value)
{
std::cout << "non fundamentaln";
}
int main()
{
Print(1); // prints "fundamental"
Print(std::string("str")); // prints "non fundamental"
}
在类型特征上应用同样的想法是行不通的。
template<fundamental T>
struct SomeTypeTrait
{
using type = T;
};
template<non_fundamental T>
struct SomeTypeTrait
{
using type = std::shared_ptr<T>;
};
using ExpectedToBeDouble = SomeTypeTrait<double>::type;
using ExpectedToBeSharedPtrOfString = SomeTypeTrait<std::string>::type; // fails to compile
我得到一个编译器错误(MSVC(说:
error C3855: 'SomeTypeTrait': template parameter 'T' is incompatible with the declaration
如何使用概念实现所需的行为?
显然,语法与我所想的略有不同。
这是一个有效的解决方案:
template<typename T>
struct SomeTypeTrait {};
template<fundamental T>
struct SomeTypeTrait<T> // note the extra <T>
{
using type = T;
};
template<non_fundamental T>
struct SomeTypeTrait<T> // note the extra <T>
{
using type = std::shared_ptr<T>;
};
此外,其中一个专业化可以成为默认实现,使代码更短,并允许稍后添加更多的专业化:
template<typename T>
struct SomeTypeTrait // default
{
using type = std::shared_ptr<T>;
};
template<fundamental T>
struct SomeTypeTrait<T> // specialization for fundamental types
{
using type = T;
};