constexpr

How will C++20 constexpr containers work?

送分小仙女□ 提交于 2020-12-25 04:07:51
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated

How will C++20 constexpr containers work?

空扰寡人 提交于 2020-12-25 04:07:36
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated

std::abs can be used in constexpr function, but only if it's templated. Why?

谁说我不能喝 提交于 2020-12-13 03:09:19
问题 Supposedly std::abs is not constexpr in the standard (even in C++20). But in practice I found out that I can compile it as constexpr under the very peculiar condition that the function is templated. See this completely working example: template<class T> constexpr T f(const T input) { return std::abs(input); } int main() { int i = -1; int a = f(i); return 0; } The code: Compiles fine with GCC, with and without the template. It doesn't work in Clang. And in Visual Studio it compiles with the

Can a constexpr function contain a label?

依然范特西╮ 提交于 2020-12-08 07:10:35
问题 This program: constexpr void f() { x: ; } is compiled by gcc, but clang says: error: statement not allowed in constexpr function So is this code valid? 回答1: As pointed out in a comment, clang is correct, and the code is ill-formed. According to the current working draft (which includes C++20), dcl.constexpr#3 says: The definition of a constexpr function shall satisfy the following requirements: ... its function-body shall not enclose ... an identifier label, ... ... 来源: https://stackoverflow

How can my code do one thing at compile-time, but another thing at run-time?

陌路散爱 提交于 2020-11-28 08:05:20
问题 I'm implementing a constexpr int foo(); function. In the body of foo() , I want to do something different (and possibly return something different) at compile time and something different at run-time. With C++20, I can use std::is_constant_evaluated: constexpr int foo() { return std::is_constant_evaluated() ? 123 : 456 }; but what if I'm using C++17 (or earlier) - what can I do with the same effect? Note: Compiler-specific solutions are acceptable (though less desirable). 回答1: Note: Compiler

How can my code do one thing at compile-time, but another thing at run-time?

北城余情 提交于 2020-11-28 08:04:44
问题 I'm implementing a constexpr int foo(); function. In the body of foo() , I want to do something different (and possibly return something different) at compile time and something different at run-time. With C++20, I can use std::is_constant_evaluated: constexpr int foo() { return std::is_constant_evaluated() ? 123 : 456 }; but what if I'm using C++17 (or earlier) - what can I do with the same effect? Note: Compiler-specific solutions are acceptable (though less desirable). 回答1: Note: Compiler

How can my code do one thing at compile-time, but another thing at run-time?

我是研究僧i 提交于 2020-11-28 07:58:05
问题 I'm implementing a constexpr int foo(); function. In the body of foo() , I want to do something different (and possibly return something different) at compile time and something different at run-time. With C++20, I can use std::is_constant_evaluated: constexpr int foo() { return std::is_constant_evaluated() ? 123 : 456 }; but what if I'm using C++17 (or earlier) - what can I do with the same effect? Note: Compiler-specific solutions are acceptable (though less desirable). 回答1: Note: Compiler