简体   繁体   English

constexpr函数可以调用返回void的函数吗?

[英]Can a constexpr function call a function that returns void?

This is what I would like to do: 这是我想做的:

void g() {
    std::cout << "smth1" << std::endl;
    std::cout << "smth2" << std::endl;
}

constexpr bool f() {return g(), true;}

The compiler ( gcc version 4.8.2) is not happy with this, since g is not constexpr . 编译器( gcc版本4.8.2)对此不满意,因为g不是constexpr Is it possible to get around this problem? 有可能解决这个问题吗?

The point of constexpr is that it can be fully expanded at compile-time. constexpr的要点是它可以在编译时完全扩展。 There is no possible way that the compiler can put runtime side effects (using cout ) into what is effectively a more complex compile-time constant. 编译器不可能将运行时的副作用(使用cout )放入实际上是更复杂的编译时常量的方式。 Put another way: No actual function call will be made at runtime for constexpr functions! 换句话说,在运行时不会对constexpr函数进行实际的函数调用!

Luckily the solution is easy! 幸运的是,解决方案很简单!

Change f to constexpr bool f() { return true;} f更改为constexpr bool f() { return true;}

and your conditional to: 并且您的条件是:

g();
if(f())
{
    // ...
}

Any evaluation of a function called during the evaluation of a constant expression must not have any side effects. 在常量表达式的求值过程中对函数的任何求值都不得有任何副作用。 In C++11, the rules are actually much stricter, allowing only a single return statement as function body but that's not relevant to this question. 在C ++ 11中,规则实际上要严格得多,只允许将一个return语句用作函数体,但这与该问题无关。

The compiler is not entitled to remove any code from a constexpr function in order to make it comply. 编译器无权从constexpr函数中删除任何代码以使其合constexpr It is your responsibility to not write such code in the first place. 您有责任不首先编写此类代码。

However, it is okay to have code with side effects inside a constexpr function if the control flow during static evaluation never passes through it. 但是,如果静态评估过程中的控制流从不通过constexpr函数,则可以在constexpr函数中包含具有副作用的代码。

This is valid C++11. 这是有效的C ++ 11。

void g() { std::cout << "hello, world" << std::endl; }
constexpr bool f(const bool p = false) { return p ? (g(), false) : true; }

And you may call f like this. 您可以这样称呼f

constexpr auto x = f();  // ok

Because p is false at compile-time, the compiler need not evaluate the call to g . 由于p在编译时为false ,因此编译器无需评估对g的调用。 At run-time, you may call f with either argument just as if it were not a constexpr function. 在运行时,您可以使用任意一个参数调用f ,就像它不是constexpr函数一样。

f(false);  // ok
f(true);   // ok, produces output at run-time 

What you cannot do is evaluate it in a constant expression with the parameter set to true . 您不能做的是使用参数设置为true的常量表达式对其求true

constexpr auto x = f(true);  // compile-time error

Of course, this example is contrived beyond any restrictions and you should simply write 当然,此示例是人为设计的,您应该编写

constexpr bool f() noexcept { return true; }

or use a variable 或使用一个变量

constexpr auto toggle = true;

if this is all you need. 如果这就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用通过 constexpr 表达式返回 void 的 constexpr 函数? - How to call a constexpr function which returns void by a constexpr expression? 是否有必要为返回void的函数声明constexpr? - Any reason to declare constexpr for a function that returns void? constexpr void函数被拒绝 - constexpr void function rejected 你可以调用一个 constexpr 函数来分配一个带有前向声明的 constexpr 值吗? - Can you call a constexpr function to assign a constexpr value with a forward declaration? 通过调用constexpr函数定义静态constexpr成员 - Defining a static constexpr member by call to constexpr function 从constexpr模板函数调用non constexpr - Call non constexpr from constexpr template function 如果没有返回,如何调用void函数? - how can a void function be called to the main, if there are no returns? 为什么我可以在constexpr函数中调用非constexpr函数? - Why can I call a non-constexpr function inside a constexpr function? 如何从 constexpr function 中未标记为 constexpr 的第三方库调用 function? - How can I call a function from a third party lib that is not marked constexpr from a constexpr function? 您不能通过创建一个返回一个的 constexpr function 来创建一个 constexpr 数组吗? - Can't you just make a constexpr array by making a constexpr function that returns one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM