【发布时间】:2019-05-29 05:43:51
【问题描述】:
我想创建一个函数模板,该函数模板专门用于可能应用了std::to_string 的类型,另一个用于我已为其定义了mylib::to_string 的类,并让任何可能不适用的类型其他实现或专业化。
例子:
// I only want this to be found if std::to_string exists for ItemType
template<typename ItemType>
void func(ItemType &i)
{
std::cout << std::to_string(i);
}
// I only want this to be found if mylib::to_string exists for ItemType
template<typename ItemType>
void func(ItemType &i)
{
std::cout << mylib::to_string(i);
}
// And finally, I'd like to be able to fall through to anything that has a << defined for streams
template<>
void func<MyClass>(MyClass &i)
{
std::cout << MySpecialConverterFunc(i);
}
std::enable_if 语法总是有问题。如何将它添加到第一个模板中?
【问题讨论】:
-
仅供参考,这么多天后编辑帖子并使现有答案无效,这在 SO 上是非常不受欢迎的。问一个新问题会更好。