尽管存在专用函数,但是否可以调用非专用模板函数?
Is it possible to call a non-specialized template function although a specialized function exists?
如果我有一个特化的模板函数,是否可以显式调用非特化模板?
template<class IntegerType>
inline IntegerType bitCount(IntegerType bitset)
{
puts("general");
return 0;
}
template<>
inline std::uint64_t bitCount(std::uint64_t bitset)
{
puts("specialized");
return 0;
}
int main() {
std::uint64_t x = 1<<5;
std::cout << bitCount(x) << '\n'; //specialized
std::cout << bitCount<std::uint64_t>(x) << '\n'; //specialized
return 0;
}
我的用例是我想编写单元测试以确保通用函数及其特化产生相同的结果。
不,专精是bitCount<std::uint64_t>
。
如果您有 #define
用于测试,则可以在测试期间超载 bitCount
template<class IntegerType>
inline IntegerType bitCount(IntegerType bitset)
{
puts("general");
return 0;
}
#ifndef(TEST_BITCOUNT)
template<>
#endif
inline std::uint64_t bitCount(std::uint64_t bitset)
{
puts("specialized");
return 0;
}
如果我有一个特化的模板函数,是否可以显式调用非特化模板?
template<class IntegerType>
inline IntegerType bitCount(IntegerType bitset)
{
puts("general");
return 0;
}
template<>
inline std::uint64_t bitCount(std::uint64_t bitset)
{
puts("specialized");
return 0;
}
int main() {
std::uint64_t x = 1<<5;
std::cout << bitCount(x) << '\n'; //specialized
std::cout << bitCount<std::uint64_t>(x) << '\n'; //specialized
return 0;
}
我的用例是我想编写单元测试以确保通用函数及其特化产生相同的结果。
不,专精是bitCount<std::uint64_t>
。
如果您有 #define
用于测试,则可以在测试期间超载 bitCount
template<class IntegerType>
inline IntegerType bitCount(IntegerType bitset)
{
puts("general");
return 0;
}
#ifndef(TEST_BITCOUNT)
template<>
#endif
inline std::uint64_t bitCount(std::uint64_t bitset)
{
puts("specialized");
return 0;
}