Why does constexpr assume that my method is const?

Writing a primitive type wrapper, I got this error from the compiler: struct Integer { constexpr Integer( int i ) : m_i( i ) { } constexpr Integer& operator++( ) { ++m_i; // error: increment of member 'Integer::m_i' in read-only object return *this;

Is it possible to get the current time at compile time?

I'm thinking about using current timestamp as a piece of version. I would like to retrieve that information at compile time. So ideally I would like to do: constexpr long long currentTimestamp = getCurrentTimestamp(); Is that possible with C++14?Use

constexpr string vs const string

We are migrating some of our code from c++03 to c++14 and wherever there is some performance gain we want to use c++14 features.Now in one of our projects we are parsing csv based on column names and these column names are declared in one header file

Copy the constexpr table into the class

Suppose I have a class: class MyClass{ char array[12]; public: MyClass(const char* arr) { for (int x = 0; x < 12; x++){ array[x] = arr[x]; } } }; Is it possible to make the MyClass constructor constexpr. The tricky part is initializing the new array.

Compile time string encryption using constexpr

I want to have a compile-time string encryption, such that I could write in my code: const auto encryptedInvalidLicense = ENCRYPT("Invalid license"); std::cout << encryptedInvalidLicense.decrypt() << std::endl; // outputs "Inval

Pass arguments to constexpr functions

I'm new into programming and I started to learn about it using the book Programming principles and practice using C++. Today I'm here because I have some problems in understanding constexpr functions. In chapter 8 the author introduces them in some l

How to detect if constexpr is supported at compile time

I am currently working in a project where we target several platforms and our codebase is built using several different compilers (gcc/clang/vc++). For our windows platform, we use VS2013 which currently does not support 'constexpr', and I was wonder

constexpr array of constexpr objects using move ctor

I have a class with a constexpr value constructor, but no copy or move ctor class C { public: constexpr C(int) { } C(const C&) = delete; C& operator=(const C&) = delete; }; int main() { constexpr C arr[] = {1, 2}; } I've found that this code d

Parameters of the constexpr function as model arguments

I am playing around with some toy code using c++11 to figure out a bit more about how things work. During this I came across the following issue that simplifies down to: template <int x, int y> class add { public: static constexpr int ret = x + y; }

using the constexpr member function in the switch

Why I can use constexp non-member function in switch, but when I use member function compiler is saying: "the value of 'y' is not usable in a constant expression"? class Test { public: constexpr Test(int i) : i(i) { } constexpr int get() {return

Toggle the case statement with the member variable in the case

I am trying to find a way to evaluate a switch-case statement using a member variable in the case part. I thought that having a global static variable like below would be allowed as const-expression. Unfortunately the compiler tells me the opposite:

Detect constexpr with SFINAE

I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like

constexpr, arrays and initialization

Is there anything in the world of C++ that would make what I'm trying to do possible? template < typename T , size_t Size > struct array { constexpr T buf[Size]; constexpr size_t size() const { return Size; } }; template < typename T , size_t Siz

Why do we need to mark the functions as constexpr?

C++11 allows functions declared with the constexpr specifier to be used in constant expressions such as template arguments. There are stringent requirements about what is allowed to be constexpr; essentially such a function encapsulates only one sube

Calculating the factorial of a small integer at compile time

I just implemented (once again) a recursive template for computing the factorial of an integer at compile time (who would had thought that some day I'll actually need it!). Still, instead of rolling my own, I went to Boost looking for an answer. Howe

Calling constexpr in the default template argument

In C++11 I am using a constexpr function as a default value for a template parameter - it looks like this: template <int value> struct bar { static constexpr int get() { return value; } }; template <typename A, int value = A::get()> struct foo