constexpr

Equivalent ternary operator for constexpr if?

孤者浪人 提交于 2019-12-04 15:15:37
问题 Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if? template<typename Mode> class BusAddress { public: explicit constexpr BusAddress(Address device) : mAddress(Mode::write ? (device.mDevice << 1) : (device.mDevice << 1) | 0x01) {} private: uint8_t mAddress = 0; }; 回答1: No, there is no constexepr conditional operator. But you could wrap the whole thing in a lambda and immediately evaluate it (an IIFE): template

Constexpr find for array using c++17

守給你的承諾、 提交于 2019-12-04 11:59:47
I am trying to write a constexpr find function that will return the index of a std::array containing a certain value. The function below seems to work OK except when the contained type is const char* : #include <array> constexpr auto name1() { return "name1"; } constexpr auto name2() { return "name2"; } template <class X, class V> constexpr auto find(X& x, V key) { std::size_t i = 0; while(i < x.size()) { if(x[i] == key) return i; ++i; } return i; } int main() { constexpr std::array<const char*, 2> x{{name1(), name2()}}; constexpr auto f1 = find(x, name1()); // this compiles constexpr auto f2

Can adding 'constexpr' change the behaviour?

前提是你 提交于 2019-12-04 10:14:42
问题 Given two programs where the only difference in the source code is the presence or absence of one constexpr , is it possible that the meaning of the program changes? In other words, if there was a compiler option to ask the compiler to try really hard to infer constexpr where possible, would it break existing standard code and/or change its meaning in bad ways? Imagine dealing with a codebase where the original developer forgot to include constexpr in places where it was possible, perhaps

In C++14 can a constexpr member change a data member?

自闭症网瘾萝莉.ら 提交于 2019-12-04 09:06:57
In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: struct myclass { int member; constexpr myclass(int input): member(input) {} constexpr void f() {member = 42;} // Is it allowed? }; Yes they are, I believe this change started with proposal N3598: constexpr member functions and implicit const and eventually became part of N3652: Relaxing constraints on constexpr functions which changed section 7.1.5 paragraph 3 what is allowed in the function body from a white-list: its function-body shall be = delete, = default, or a

When would I use std::integral_constant over constexpr?

天大地大妈咪最大 提交于 2019-12-04 08:02:24
问题 #include <iostream> #include <type_traits> int main(){ //creating an integral constant with constexpr constexpr unsigned int speed_of_light{299792458}; //creating an integral constant with std::integral_constant typedef std::integral_constant<unsigned int, 299792458> speed_of_light_2; //using them std::cout << speed_of_light/2 << '\n'; std::cout << speed_of_light_2::value/2 << '\n'; } What's special about std::integral_constant that I would choose to use it over constexpr ? Their behaviour

Filling a std::array at compile time and possible undefined behaviour with const_cast

笑着哭i 提交于 2019-12-04 07:47:54
It is known that std::array::operator[] since C++14 is constexpr , see declaration below: constexpr const_reference operator[]( size_type pos ) const; However, it is also const qualified. This causes implications if you want to use the subscript operator of a std::array in order to assign values to your array at compile time. For example consider the following user literal: template<typename T, int N> struct FooLiteral { std::array<T, N> arr; constexpr FooLiteral() : arr {} { for(int i(0); i < N; ++i) arr[i] = T{42 + i}; } }; The above code won't compile if you try to declare a constexpr

Why does the compiler complain about this not being a constexpr?

心已入冬 提交于 2019-12-04 04:28:13
I am trying to learn a bit more on how to use C++ constant expressions in practice and created the following Matrix class template for illustration purposes: #include <array> template <typename T, int numrows, int numcols> class Matrix{ public: using value_type = T; constexpr Matrix() : {} ~Matrix(){} constexpr Matrix(const std::array<T, numrows*numcols>& a) : values_(a){} constexpr Matrix(const Matrix& other) : values_(other.values_){ } constexpr const T& operator()(int row, int col) const { return values_[row*numcols+col]; } T& operator()(int row, int col){ return values_[row*numcols+col]; }

MSVC 2017 violating static initialization order within single translation unit

岁酱吖の 提交于 2019-12-04 04:03:33
问题 MSVC 2017 Community with -std=c++17 chokes on the following example: #include <iostream> struct TC { static TC const values[]; static TC const& A; static TC const& B; static TC const& C; int const _value; }; inline constexpr TC const TC::values[]{ { 42 }, { 43 }, { 44 } }; inline constexpr TC const& TC::A{ values[0U] }; inline constexpr TC const& TC::B{ values[1U] }; inline constexpr TC const& TC::C{ values[2U] }; int main(int, char**) noexcept { std::cout << std::boolalpha << "&A == &values

C++11 - Can't define constexpr literal using constexpr function?

本秂侑毒 提交于 2019-12-04 04:01:06
问题 I've run into what seems a counterintuitive error, namely, the inability to assign the value of a constexpr function to a constexpr literal (hope I'm using the language right). Here's the example: class MyClass { public: static constexpr int FooValue(int n) { return n + 5; } static constexpr int Foo5 = FooValue(5); // compiler error static constexpr int Foo5Alt(void) { return FooValue(5); } // OK }; In GCC 4.8.4, Foo5 is flagged for field initializer is not constant . Found this thread

Replacing constants: when to use static constexpr and inline constexpr?

旧街凉风 提交于 2019-12-04 03:50:17
问题 This question is a followup question to C++17: still using enums as constants?. Legacy constants come in several forms, notably: #define CONSTANT x enum { CONSTANT = x }; const /*int/unsigned/whatever*/ CONSTANT = x; A comment about static constexpr and inline constexpr constants as a replacement got me thinking on the subject of updating our many, many legacy constants (particularly #define constants). As I understand, an inline constexpr value is basically just substituted in place, like an