constexpr

What is the purpose of marking the set function (setter) as constexpr? [duplicate]

大兔子大兔子 提交于 2020-02-01 17:41:07
问题 This question already has answers here : What is the use of a constexpr on a non-const member function? (2 answers) Closed 12 months ago . I cannot understand the purpose of marking the setter function as constexpr , that is allowed since C++14. My misunderstanding comes from the next situation: I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1 . An object p1 now is constant and its

Comparing two constexpr pointers is not constexpr?

↘锁芯ラ 提交于 2020-01-23 08:01:27
问题 I am looking for a way to map types to numeric values at compile time, ideally without using a hash as proposed in this answer. Since pointers can be constexpr , I tried this: struct Base{}; template<typename T> struct instance : public Base{}; template<typename T> constexpr auto type_instance = instance<T>{}; template<typename T> constexpr const Base* type_pointer = &type_instance<T>; constexpr auto x = type_pointer<int> - type_pointer<float>; // not a constant expression Both gcc and clang

adapting a non-constexpr integral value to a non-type template parameter, and code bloat

一笑奈何 提交于 2020-01-21 07:30:29
问题 Consider a function object F taking a constexpr size_t argument I struct F { template <size_t I> constexpr size_t operator()(size <I>) const { return I; } }; wrapped within a type size <I> , where (for brevity) template <size_t N> using size = std::integral_constant <size_t, N>; Of course, we could pass I directly but I want to emphasize that it is constexpr by using it as a template argument. Function F is dummy here but in reality it could do a variety of useful stuff like retrieving

Why is std::array::size constexpr with simple types (int, double, …) but not std::vector (GCC)?

拥有回忆 提交于 2020-01-21 04:07:04
问题 The following code: std::array<int, 4> arr1; std::array<float, arr1.size()> arr2; ...compiles with both gcc and clang because std::array::size is considered constexpr . But the following does not compile with gcc (version 5.3.0 20151204): std::array<std::vector<int>, 4> arr1; std::array<std::vector<double>, arr1.size()> arr2; For me, there is no reason such code should fail to compile if the first one works but since I did not find a lot of post on this I don't know if it is a gcc bug or a

Can an exception be thrown from the ternary operator?

那年仲夏 提交于 2020-01-19 05:37:49
问题 Sometimes it is convenient or even necessary to have a function which just one statement (it is necessary when returning a constexpr ). If a condition needs to be checked and only one statement is allowed, the conditional operator is the only option. In case of an error it would be nice to throw an exception from the conditional operator, e.g.: template <typename It> typename std::iterator_traits<It>::reference access(It it, It end) { return it == end? throw std::runtime_error("no element"):

Constexpr member function

大城市里の小女人 提交于 2020-01-14 10:28:08
问题 Suppose I have a struct template S that is parametrized by an engine: template<class Engine> struct S; I have two engines: a "static" one with a constexpr member function size() , and a "dynamic" one with a non- constexpr member function size() : struct Static_engine { static constexpr std::size_t size() { return 11; } }; struct Dynamic_engine { std::size_t size() const { return size_; } std::size_t size_ = 22; }; I want to define size() member function in S that can be used as a constexpr if

How do I make a constexpr swap function?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 00:31:48
问题 I'm making my own String View class for learning purposes, and I'm trying to make it 100% constexpr. To test it, I have a member function that returns an hash value. I then construct my string view in a switch statement and call that same member function, if it passes, that member function has fullfiled its purpose. To learn, I'm using / reading / comparing my implementation with Visual Studio 2017 latest update std::string_view , however, I've noticed that, despite swap being marked as

Switch in constexpr function

血红的双手。 提交于 2020-01-13 12:16:12
问题 Found the following statement in Wiki: C++11 introduced the concept of a constexpr-declared function; a function which could be executed at compile time. Their return values could be consumed by operations that require constant expressions, such as an integer template argument. However, C++11 constexpr functions could only contain a single expression that is returned (as well as static_asserts and a small number of other declarations). C++14 relaxes these restrictions. Constexpr-declared

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

旧街凉风 提交于 2020-01-12 14:40:55
问题 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 {

Constexpr and SSE intrinsics

半世苍凉 提交于 2020-01-12 07:20:31
问题 Most C++ compilers support SIMD(SSE/AVX) instructions with intrisics like _mm_cmpeq_epi32 My problem with this is that this function is not marked as constexpr , although "semantically" there is no reason for this function to not be constexpr since it is a pure function. Is there any way I could write my own version of (for example) _mm_cmpeq_epi32 that is constexpr ? Obviously I would like that the function at runtime uses the proper asm, I know I can reimplement any SIMD function with slow