模板约束可用于变量模板吗? [英] Will template constraints be available for variable templates?

查看:196
本文介绍了模板约束可用于变量模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的模板约束中,提出了一个用于约束模板参数的新工具集。此外,在C ++ 14中提供了变量模板。变量模板允许定义类型参数化的常量等。

In the latest template constraints paper a new toolset to constrain template arguments is presented. Also, in C++14 variable templates are provided. Variable templates allow the definition of type parameterized constants among other things.

没有提到这些特性如何交互。使用pi的规范示例,我们可以具有:

There is no mention of how these feature could interact. Using the canonical example of pi we could have this:

template<Integral T>
  constexpr double pi(3.141592653589793238);

template<Floating_point T>
  constexpr T pi(3.1415926535897932384626433832795029L);

这将强制执行从整数到双精度的C / C ++数值转换。它也将防止实例化与完全不相关的类型。 (看看这个,我们可能需要替换Floating_point需要一个浮点ctor以支持复杂。)

This would enforce the C/C++ numeric conversion from integral to double. It would also prevent instantiation with totally irrelevant types. (Looking at this, we might want to replace Floating_point with something that requires a floating point ctor in order to support complex.)

我错过了一篇论文或者是在作品中?

Did I miss something in one of the papers or is this in the works? Maybe it comes for free and is not worth mentioning?

推荐答案

在最新版本的提案( N4040 ,日期为2014年5月) ,则答案为

In the latest version of the proposal (N4040, dated May 2014), the answer would be NO:


5 变量模板限制:

- 模板必须是不受约束

- 声明的类型必须为bool。

— The declared type must be bool.

- 声明必须有一个初始值设定器。

— The declaration must have an initializer.

- 初始值为约束表达式。

— The initializer shall be a constraint-expression.

[示例:



template<typename T>
concept bool D1 = has_x<T>::value; // OK

template<typename T>
concept bool D2 = 3 + 4;           // Error: initializer is not a constraint

template<Integral T>
concept bool D3 = has_x<T>::value; // Error: constrained concept definition




>

— end example ]

这篇关于模板约束可用于变量模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆