已经被告知模板中的[[逻辑与(&&
)不起作用,所以我想使用模板专业化来实现它。
#include <iostream>
template <bool b1, bool b2>
constexpr static bool andVal = false;
// template specialization
template <bool b2>
constexpr static bool andVal<true, b2> = b2;
int main(int argc, char *argv[]) {
std::cout << andVal<1, 1> << std::endl;
std::cout << andVal<0, 1> << std::endl;
std::cout << andVal<0, 0> << std::endl;
std::cout << andVal<1, 0> << std::endl; // this line will cause compilation error
return 0;
}
但是当我编译代码时,发生了这样的错误:
/tmp/ccaqDdfO.s: Assembler messages: /tmp/ccaqDdfO.s:369: Error: symbol `_ZL6andVal' is already defined
如果我注释最后一行测试代码std::cout << andVal<1, 1> << std::endl;
,则编译将成功并且测试结果正确。模板功能出了什么问题?以及为什么已经定义了?
任何回复将不胜感激!