c ++模板短路逻辑与(&&)

问题描述 投票:1回答:1

已经被告知模板中的[[逻辑与(&&)不起作用,所以我想使用模板专业化来实现它。

我的测试代码如下:

#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;,则编译将成功并且测试结果正确。

模板功能出了什么问题?以及为什么已经定义了?

任何回复将不胜感激!

c++ templates short-circuiting
1个回答
1
投票
模板很好。这是gcc 5.4至6.1版本的错误:template specialization compile error。您恰好位于错误版本范围的底端。
© www.soinside.com 2019 - 2024. All rights reserved.