constant-expression

Why references can't be used with compile time functions?

左心房为你撑大大i 提交于 2021-02-08 05:30:10
问题 I have two snippets. The first snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; if constexpr (foo(a)) { } } The second snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; std::string& x = a; if constexpr (foo(x)) { } } The first one compiles, but the second one does not compile (error message: error: the value of ‘x’ is not usable in a constant expression .

Can array length in declaration be non-constant?

夙愿已清 提交于 2021-02-05 08:41:09
问题 I am a bit confused about array declaration in C. I know that it's possible to do this: int a[20]; // Reserved space for 20 int array int b[] = {32, 431, 10, 42}; // Length in square brackets is auto-calculated int *c = calloc(15, sizeof(int)); // Created a pointer to the dynamic int array But is it possible to do this?: int my_array[sizeof(int) * 5]; Is it a valid code, or an array length should be a constant expression (in ANSI C)? 回答1: sizeof(int) * 5 used in the example statement in your

Can array length in declaration be non-constant?

那年仲夏 提交于 2021-02-05 08:40:38
问题 I am a bit confused about array declaration in C. I know that it's possible to do this: int a[20]; // Reserved space for 20 int array int b[] = {32, 431, 10, 42}; // Length in square brackets is auto-calculated int *c = calloc(15, sizeof(int)); // Created a pointer to the dynamic int array But is it possible to do this?: int my_array[sizeof(int) * 5]; Is it a valid code, or an array length should be a constant expression (in ANSI C)? 回答1: sizeof(int) * 5 used in the example statement in your

Can I get a Rust array's length with only a type, not a concrete variable?

纵然是瞬间 提交于 2020-12-06 07:06:52
问题 I want to rewrite the following C++ code into Rust: using storage = array<int, 3>; const size_t storage_len = sizeof(storage) / sizeof(storage::value_type); How can I get that constant length value without a concrete variable? As motivation, although it may seem trivial, I want to print the array's element count without declaring a variable. I know I could use a constant value or declare a dummy variable, but I wonder how Rust can preserve C++ code. I admit without a concrete variable is not

Templated delegating copy constructor in constant expressions

荒凉一梦 提交于 2020-03-21 17:55:34
问题 This question is motivated by this one. Consider the following code: struct B {}; struct S { B b; // #1 S() = default; template <typename ...dummy> // #2 constexpr S(const S&) {} template <typename ...dummy> // #3 constexpr S(S &other) : S(const_cast<const S&>(other)) // #4 {} }; S s; constexpr S f() {return s;} int main() { constexpr auto x = f(); } GCC compiles this code successfully, but Clang rejects it (Example on Godbolt.org). The error message produced by Clang is <source>:21:20: error

Templated delegating copy constructor in constant expressions

本小妞迷上赌 提交于 2020-03-21 17:55:12
问题 This question is motivated by this one. Consider the following code: struct B {}; struct S { B b; // #1 S() = default; template <typename ...dummy> // #2 constexpr S(const S&) {} template <typename ...dummy> // #3 constexpr S(S &other) : S(const_cast<const S&>(other)) // #4 {} }; S s; constexpr S f() {return s;} int main() { constexpr auto x = f(); } GCC compiles this code successfully, but Clang rejects it (Example on Godbolt.org). The error message produced by Clang is <source>:21:20: error

Why is a constexpr function on a reference not constexpr?

◇◆丶佛笑我妖孽 提交于 2020-01-21 10:59:04
问题 Consider the following function: template <size_t S1, size_t S2> auto concatenate(std::array<uint8_t, S1> &data1, std::array<uint8_t, S2> &data2) { std::array<uint8_t, data1.size() + data2.size()> result; auto iter = std::copy(data1.begin(), data1.end(), result.begin()); std::copy(data2.begin(), data2.end(), iter); return result; } int main() { std::array<uint8_t, 1> data1{ 0x00 }; std::array<uint8_t, 1> data2{ 0xFF }; auto result = concatenate(data1, data2); return 0; } When compiled using

Why can creating a static const std::string cause an exception?

孤街醉人 提交于 2020-01-12 11:51:15
问题 I have string constants, for strings that I use in multiple places in my app: namespace Common{ static const std::string mystring = "IamAwesum"; } When posting a question about something else (What happens to a .h file that is not included in a target during compilation?), another user made the following comment : be aware that your static string are global in this case. So they are could create an exception at anytime and can't be catch. I advise you to use function who return a reference of

Is assert usable in constant expressions?

六眼飞鱼酱① 提交于 2020-01-02 00:57:10
问题 The assert -macro from <cassert> provides a concise way of ensuring that a condition is met. If the argument evaluates to true , it shall not have any further effects. However, can its invocation also be used inside a constant expression in that case? 回答1: This was dealt with by LWG 2234, which was brought back to attention after relaxed constraints on constexpr functions had been introduced. Proposed resolution : This wording is relative to N3936. Introduce the following new definition to

Variable cannot appear in a constant-expression

筅森魡賤 提交于 2019-12-24 10:23:30
问题 I'm having a hard time figuring out why GCC 4.5 won't let me compile this: #include <iostream> #include <bitset> #define WIDTH 512 #define HEIGHT 512 #define CEIL_POS(X) ((X - (unsigned int)(X)) > 0 ? (unsigned int)(X + 1) : (unsigned int)(X)) int main () { const unsigned int length = static_cast<const unsigned int>(CEIL_POS(static_cast<float>(WIDTH * HEIGHT) / 8.0)); std::bitset<length> bits; return 0; } It works just fine in VS2010. What am I missing? UPDATE: I was in a hurry and I didn't