Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 什么时候在constexpr上使用std::integral_常量? #包括 #包括 int main(){ //使用constexpr创建整型常量 constexpr无符号整数光速{299792458}; //使用std::integral\u常量创建整数常量 typedef std::积分式光速; //使用它们 std::cout_C++_Templates_C++11_Constants_Constexpr - Fatal编程技术网

C++ 什么时候在constexpr上使用std::integral_常量? #包括 #包括 int main(){ //使用constexpr创建整型常量 constexpr无符号整数光速{299792458}; //使用std::integral\u常量创建整数常量 typedef std::积分式光速; //使用它们 std::cout

C++ 什么时候在constexpr上使用std::integral_常量? #包括 #包括 int main(){ //使用constexpr创建整型常量 constexpr无符号整数光速{299792458}; //使用std::integral\u常量创建整数常量 typedef std::积分式光速; //使用它们 std::cout,c++,templates,c++11,constants,constexpr,C++,Templates,C++11,Constants,Constexpr,Templateintegral\u constant定义一个类型,关键字constepr定义一个常量。 例如,std::true\u type是std::integral\u常量 使用示例之一是标记分派 #include <iostream> #include <type_traits> int main(){ //creating an integral constant with constexpr constexpr unsigned int

Template
integral\u constant
定义一个类型,关键字
constepr
定义一个常量。 例如,
std::true\u type
std::integral\u常量

使用示例之一是
标记分派

#include <iostream>
#include <type_traits>

int main(){

    //creating an integral constant with constexpr
    constexpr unsigned int speed_of_light{299792458};

    //creating an integral constant with std::integral_constant
    typedef std::integral_constant<unsigned int, 299792458> speed_of_light_2;

    //using them
    std::cout << speed_of_light/2 << '\n';
    std::cout << speed_of_light_2::value/2 << '\n';

}
模板
void use_impl(常量T&,标准::false_类型)
{
}
模板
void use_impl(常量T&,标准::true_类型)
{
}
模板
无效使用(常数T&v)
{
使用_impl(v,typename std::is_integral::type());
}

它可以与三元运算符一起使用 乙二醇

void gotoN\u impl(std::integral\u constant::type)
{

std::cout但如果std::true_类型定义为"constexpr bool true_type=true;我看不出这会如何改变它的行为。也许constexpr创建了一个实际的实例,但整型常量只是定义了一个类型……不,我不这么认为。我不确定。@TrevorHickey它不会有另一个类型,在constexpr的情况下,它只会有bool类型。哦,根据comp的值做出决定ile时间。我想知道是否可以使用三元运算符和constexpr匹配该行为。不管怎样,我现在看到了它的值。一般情况下不是这样。编译器(至少是gcc)将尝试实例化所有“可访问”中的对象块,即使优化器稍后会根据constexpr条件剔除这些块。这不会发生在模板化表达式中,因为SFINAE推理是事先发生的。我从来没有理解过
std::integral\u constant
用于生成整型常量的用例。它是一种TMP功能,用于各种类型的精简表达式gs,尤其是标记分派(通常基于其专门化
std::true\u type
std::false\u type
),常量值不能用于。当然,常量使用实际常量,而不是一些奇怪的定型常量。在
template<typename T>
void use_impl(const T&, std::false_type)
{
}

template<typename T>
void use_impl(const T&, std::true_type)
{
}

template<typename T>
void use(const T& v)
{
   use_impl(v, typename std::is_integral<T>::type());
}
void gotoN_impl(std::integral_constant<int,0>::type)
{
    std::cout << "GoTo 0" << '\n';
}

void gotoN_impl(std::integral_constant<int,1>::type)
{
    std::cout << "GoTo 1" << '\n';
}

void gotoN_impl(std::integral_constant<int,2>::type)
{
    std::cout << "GoTo 2" << '\n';
}

void gotoN_impl(std::integral_constant<int,3>::type)
{
    std::cout << "GoTo 3" << '\n';
} 

template<int N>
void gotoN()
{
    gotoN_impl(typename std::integral_constant<int, N>::type());
}


int main()
{
    gotoN<0>();
    gotoN<1>();
    gotoN<2>();
    gotoN<3>();

    constexpr auto x = 99;

    gotoN<x<4?x:3>(); // with a ternary operator
}