Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+;中常量表达式的模板int参数+;11_C++_Templates_C++11_Constexpr - Fatal编程技术网

C++ C+;中常量表达式的模板int参数+;11

C++ C+;中常量表达式的模板int参数+;11,c++,templates,c++11,constexpr,C++,Templates,C++11,Constexpr,我有一个模板类: template <int N> class Object<N> { // ... } 我必须在C++11中完成它,但不幸的是不是在C++14中(其中std::max是constepr)。在C++11中可能吗?您可以使用三元运算符: template <int N1, int N2> Object<(N1 > N2)? N1 : N2> AddObjects(const Object<N1> & o

我有一个模板类:

template <int N>
class Object<N>
{
// ...
}

我必须在C++11中完成它,但不幸的是不是在C++14中(其中
std::max
constepr
)。在C++11中可能吗?

您可以使用三元运算符:

 template <int N1, int N2>
 Object<(N1 > N2)? N1 : N2> AddObjects(const Object<N1> & object_1, const Object<N2> & object_2) {
        // ...
 }
模板
对象(N2)?N1:N2>添加对象(常量对象和对象_1,常量对象和对象_2){
// ...
}

Wow!从来没有想过这样的事情是可能的你仍然可以写你自己的
constexpr
函数
max
@Jarod42请,如果在c++11中不能使用,如果在constexpr函数中不能使用,如何写它?在c++11中,三元运算符似乎是必需的(我认为对于
int
/
uint
的无分支版本有一些黑客攻击),在c++14中,
constexpr
规则允许if-else:-)
 template <int N1, int N2>
 Object<(N1 > N2)? N1 : N2> AddObjects(const Object<N1> & object_1, const Object<N2> & object_2) {
        // ...
 }