Will template constraints be available for variable templates?

戏子无情 提交于 2019-12-10 22:27:31

问题


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.

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);

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?


回答1:


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

5 A variable template has the following restrictions:

— The template must be unconstrained.

— The declared type must be bool.

— The declaration must have an initializer.

— The initializer shall be a constraint-expression.

[ Example:

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 ]



来源:https://stackoverflow.com/questions/17865724/will-template-constraints-be-available-for-variable-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!