可变模板部分专业化和constexpr
variable template partial specialization and constexpr
我试图理解模板和变量模板。考虑一下:
template<int M, int N>
const int gcd1 = gcd1<N, M % N>;
template<int M>
const int gcd1<M, 0> = M;
std::cout << gcd1<9, 6> << "n";
它打印了 0
,这是错误的。但是,如果我使用上面的constexpr
代替const
,则获得了正确的答案3
。我再次使用结构模板得到正确的答案:
template<int M, int N>
struct gcd2 {
static const int value = gcd2<N, M % N>::value;
};
template<int M>
struct gcd2<M, 0> {
static const int value = M;
};
std::cout << gcd2<9, 6>::value << "n";
我在做什么错?
编辑: gcd1
也没有基本案例专业化。怎么会?我正在使用Visual Studio2015。
我想这是 msvc 编译器中的错误。
根据此页面,由于 MSVC 2015 Update 2 ,应可用变量模板。似乎它们即使在更新3 中也无法正常工作。
无论如何,您的代码与 gcc 6.1 :wandbox
相关文章:
- 如何使用默认参数等选择模板专业化
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 模板化建造师专业化
- 多成员Constexpr结构初始化
- 条件constexpr函数
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 类模板的成员功能的定义在单独的TU中完全专业化
- Visual C++ constexpr Hints
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么constexpr的性能比正常表达式差
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 为什么std::isnan 不是 constexpr?
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 更多constexpr容器是否需要mark_immutable_if_consexpr
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 可变模板部分专业化和constexpr
- 如何定义模板类专业化的静态constexpr数组成员
- 我可以只为特定的专业化定义静态constexpr数据成员吗