定义之前使用constexpr函数失败 [英] Use of constexpr function before definition fails

查看:173
本文介绍了定义之前使用constexpr函数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些麻烦与 constexpr 。书 C ++ Primer 显示一行代码:

  constexpr int sz = size //只有size()是一个constexpr函数
//这段代码是正确的

这本书没有给出具体的例子。所以我自己尝试下面的代码:

  #include< iostream> 
constexpr int fun();
int main()
{
constexpr int f = fun();
std :: cout<< f<< std :: endl;
}
constexpr int fun()
{
return 3;但是我的编译器说 fun()










如果我将 constexpr 更改为 const ,则code未定义



< c $ c>,它工作得很好,如果我改变我的代码在使用之前定义constexpr函数:

  #include< ; iostream> 
constexpr int fun()
{
return 3;
}
int main()
{
constexpr int f = fun();
std :: cout<< f<< std :: endl;
}

有人可以告诉我为什么?

解决方案

一个 constexpr



源代码:C ++标准草案n4296,第5.20节:

/ p>


条件表达式 e 核心常量表达式,除非遵循抽象机器的规则的 e 的求值将评估以下表达式之一:




  • this ,除了 constexpr 作为 e ;

  • 的一部分进行评估的 constexpr 构造函数对于字面类, constexpr 函数的一个 constexpr 构造函数,或者隐式调用析构函数[注意:过载解析应用为
    usual - end note];

  • 调用未定义的 constexpr function 或未定义的 constexpr 构造函数;

  • ...


版本从草案3485(第5.19节):


<除非涉及以下之一作为潜在求值的子表达式,否则条件表达式核心常量表达式,但是逻辑AND,逻辑OR和条件操作的子表达式不会被考虑[注意:重载的操作符调用一个函数。 - end note]:




  • this [注意:函数调用替换在 constexpr 成员函数中用指向类对象的指针替换每次出现的 this 。 - end note];

  • 调用函数而不是 constexpr 构造函数用于文字类或 constexpr function [注意:过载分辨率像平常一样应用 - end note];

  • 调用未定义的 constexpr function 或未定义的 constexpr 构造函数

  • ...


示例 int x2 = s。 t(); 在n2235实际上变得有效,由于在标准化之前做出的更改。但是, constexpr int x2 = s。 t(); 仍然是错误。


I'm having some trouble with constexpr. The book C++ Primer shows a line of code:

  constexpr int sz = size(); // only size() is a constexpr function
                             // this code is right

However the book doesn't give a specific example. So I try the following code by myself:

#include <iostream>
constexpr int fun();
int main()
{
    constexpr int f = fun();
    std::cout << f << std::endl;
}
constexpr int fun()
{
    return 3;
}

But my compiler said fun() is undefined.

If I change constexpr into const, it works well, and if I change my code to define the constexpr function before use:

#include <iostream>
constexpr int fun()
{
    return 3;
}
int main()
{
    constexpr int f = fun();
    std::cout << f << std::endl;
}

It also works well. Can someone tell me why?

解决方案

A constexpr function does NOT have to be defined before its first use, however the result of any call made prior to definition is not a constant expression.

Source: C++ Standard draft n4296, section 5.20:

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:

  • this, except in a constexpr function or a constexpr constructor that is being evaluated as part of e;
  • an invocation of a function other than a constexpr constructor for a literal class, a constexpr function, or an implicit invocation of a trivial destructor [ Note: Overload resolution is applied as usual — end note ];
  • an invocation of an undefined constexpr function or an undefined constexpr constructor;
  • ...

version from draft 3485 (section 5.19):

A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression, but subexpressions of logical AND, logical OR, and conditional operations that are not evaluated are not considered [ Note: An overloaded operator invokes a function. — end note ]:

  • this [ Note: when evaluating a constant expression, function invocation substitution replaces each occurrence of this in a constexpr member function with a pointer to the class object. — end note ];
  • an invocation of a function other than a constexpr constructor for a literal class or a constexpr function [ Note: Overload resolution is applied as usual — end note ];
  • an invocation of an undefined constexpr function or an undefined constexpr constructor
  • ...

The example int x2 = s. t(); in n2235 actually became valid due to the changes made prior to Standardization. However, constexpr int x2 = s. t(); remains an error.

这篇关于定义之前使用constexpr函数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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