代码之家  ›  专栏  ›  技术社区  ›  camino

constexpr上下文中constexpr函数中的所有函数都必须是constexpr函数吗?

c++
  •  5
  • camino  · 技术社区  · 6 年前

    在ubuntu gcc 8.0中:

    void bar(){}
    
    constexpr int foo(int a)
    {
        if (a <=0 )
            bar();
    
        return 1;
    }
    
    int main()
    {
        int a1[foo(-1)]; //will give a compile error, which is expected, 
                         //since non-constexpr function is not allowd in constexpr context
    }
    

    int main()
    {
        int a2[foo(1)];  //no compile error
    }
    

    这里,bar是非constexpr函数。 我想知道为什么在constexpr上下文中允许非constexpr函数,尽管在这个测试中它没有被调用。

    2 回复  |  直到 6 年前
        1
  •  6
  •   Community CDub    5 年前

    constexpr上下文中constexpr函数中的所有函数都必须是constexpr函数吗?

    视情况而定。

    允许调用非constexpr函数的事实 constexpr 函数并不意味着 常量表达式 常量表达式 常量表达式 需要(如在数组边界中),调用 常量表达式 常量表达式

    [dcl.constexpr]/5

    如果不存在参数值,则函数或构造函数的调用可以是核心常量表达式的求值子表达式(8.20) ,或者,对于构造函数,某个对象的常量初始值设定项(6.6.2),程序的格式不正确,不需要诊断。

    [expr.const]/2

    表达式e是一个核心常量表达式,除非根据抽象机器(4.6)的规则,对e的求值将求值下列表达式之一:

    • (2.2)对除文本类的constexpr构造函数、constexpr函数以外的函数的调用,或对普通析构函数[…]的隐式调用

    这意味着 常量表达式 常量表达式 foo(1) bar() 但事实并非如此 foo(-1)

        2
  •  -1
  •   myhaspldeep    6 年前

    使用c++14标准编译代码:

    $ 
    $ g++ main.cpp -std=c++14
    $ ./a.out
    1
    $ cat main.cpp
    //
    //  main.cpp
    //
    //
    //  Created by myhaspl on 2018/10/24.
    //  myhaspl@myhaspl.com.
    //
    
    #include <iostream>
    using namespace std;
    
    void bar(){}
    
    constexpr int foo(int a)
    {
        if (a <=0 )
            bar();
    
        return 1;
    }
    
    int main()
    {
        int a1[2]={0,1};
    
        cout<<a1[foo(-1)]<<endl;
    }