constexpr

Why can I call a non-constexpr function inside a constexpr function?

删除回忆录丶 提交于 2019-11-27 14:02:05
Consider the following code: #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } I would have expected the compiler to complain about the call to printf inside f , because f is supposed to be constexpr , but printf is not. Why does the program compile and print 15 ? Shafik Yaghmour The program is ill-formed and requires no diagnostic according to the C++11 draft standard section 7.1.5 The constexpr specifier paragraph 5 which says: For a constexpr function, if no function argument values exist such that the function

What is Allowed in a constexpr Function?

大兔子大兔子 提交于 2019-11-27 12:28:08
问题 constexpr functions are not supposed to contain: A definition of a variable of non-literal type But in this answer a lambda is defined in one: https://stackoverflow.com/a/41616651/2642059 template <typename T> constexpr auto make_div(const T quot, const T rem) { return [&]() { decltype(std::div(quot, rem)) result; result.quot = quot; result.rem = rem; return result; }(); } and in my comment I define a div_t in one: How can I Initialize a div_t Object? template <typename T> constexpr decltype

C++ Linker Error With Class static constexpr

这一生的挚爱 提交于 2019-11-27 12:17:54
I am compiling the following simple program with g++-4.6.1 --std=c++0x : #include <algorithm> struct S { static constexpr int X = 10; }; int main() { return std::min(S::X, 0); }; I get the following linker error: /tmp/ccBj7UBt.o: In function `main': scratch.cpp:(.text+0x17): undefined reference to `S::X' collect2: ld returned 1 exit status I realize that inline-defined static members do not have symbols defined, but I was under the (probably flawed) impression that using constexpr told the compiler to always treat the symbol as an expression; so, the compiler would know that it is not legal to

How to check if two types are same at compiletime(bonus points if it works with Boost strong typedef)

十年热恋 提交于 2019-11-27 11:37:15
问题 I was wondering if it is possible to check if 2 types are same at compile time. What I came up with is(idk if it works because it feels hackish and IDK standard that good so IDK what to look for when testing). #include <boost/strong_typedef.hpp> BOOST_STRONG_TYPEDEF(double, cm); BOOST_STRONG_TYPEDEF(double, inch); template<typename T, typename U> static constexpr void __help() { } template<typename T, typename U> class AreSameType { public: constexpr operator bool() { return &__help<T,U> == &

enum vs constexpr for actual static constants inside classes

我们两清 提交于 2019-11-27 11:11:06
Let me start by stating my intent. In the olden (C++) days, we would have code like: class C { public: enum {SOME_VALUE=27}; }; Then we could use SOME_VALUE throughout our code as a compile time constant and wherever the compiler would see C::SOME_VALUE , it would just insert the literal 27. Now days, it is seems more acceptable to change that code to something like: class C { public: static constexpr int SOME_VALUE=27; }; This looks much cleaner, gives SOME_VALUE a well defined type and seems to be the preferred approach as of C++11. The (unforseen at least for me) problem is that this also

Passing constexpr objects around

大憨熊 提交于 2019-11-27 09:45:53
I decided to give then new C++14 definition of constexpr a spin and to get the most out of it I decided to write a little compile-time string parser. However, I'm struggling with keeping my object a constexpr while passing it to a function. Consider the following code: #include <cstddef> #include <stdexcept> class str_const { const char * const p_; const std::size_t sz_; public: template <std::size_t N> constexpr str_const( const char( & a )[ N ] ) : p_( a ), sz_( N - 1 ) {} constexpr char operator[]( std::size_t n ) const { return n < sz_ ? p_[ n ] : throw std::out_of_range( "" ); } constexpr

What am I allowed to do with a static, constexpr, in-class initialized data member?

核能气质少年 提交于 2019-11-27 09:42:06
This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data member (a book mentioned this to be illegal). Johannes Schaub's answer states, that: This violates the One

constexpr not working if the function is declared inside class scope

霸气de小男生 提交于 2019-11-27 09:25:31
I am using g++4.8.0, which doesn't contain earlier constexpr bug. Thus below code works fine : constexpr int size() { return 5; } int array[size()]; int main () {} However, if I enclose both the variable inside a class as static , then it gives compiler error : struct X { constexpr static int size() { return 5; } static const int array[size()]; }; int main () {} Here is the error: error: size of array ‘array’ is not an integral constant-expression Is it forbidden to use constexpr in such a way or yet another g++ bug? Yes, it is ill-formed. Here's why: A constexpr function needs to be defined

Throw in constexpr function

心不动则不痛 提交于 2019-11-27 09:24:09
The following piece of code compiles under clang++ 3.7.0 but is denied by g++ 5.3.1. Both have -std=c++14 option. Which compiler is correct? Anyone knows where in the standard talks about this? Thanks. #include <stdexcept> using namespace std; constexpr int f(int n) { if (n <= 0) throw runtime_error(""); return 1; } int main() { char k[f(1)]; } Output [hidden] g++ -std=c++14 c.cpp c.cpp: In function ‘constexpr int f(int)’: c.cpp:7:1: error: expression ‘<throw-expression>’ is not a constant-expression } ^ [hidden] clang++ -std=c++14 c.cpp [hidden] [hidden] g++ -v Using built-in specs. COLLECT

constexpr if and static_assert

喜欢而已 提交于 2019-11-27 09:01:02
P0292R1 constexpr if has been included , on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me: Disarming static_assert declarations in the non-taken branch of a constexpr if is not proposed. void f() { if constexpr (false) static_assert(false); // ill-formed } template<class T> void g() { if constexpr (false) static_assert(false); // ill-formed; no // diagnostic required for template definition } I take it that it's completely forbidden to use static_assert inside