constexpr

If statement failing to evaluate condition

断了今生、忘了曾经 提交于 2019-12-11 06:06:03
问题 I have a basic class that containers two enumerators, one for input and one for output. It has two member functions which are both static. The first function is just a static function that returns a value based on the input. It will call the second function which is a constexpr function template that will return the constexpr values. You can see the full class here. class Foo { public: enum Input { INPUT_0 = 0, INPUT_1, INPUT_2 }; enum Output { OUTPUT_0 = 123, OUTPUT_1 = 234, OUTPUT_2 = 345 }

Error when declaring static constexpr instance of derived class in CRTP base class

亡梦爱人 提交于 2019-12-11 05:28:27
问题 I'm building a container type and attempting to reuse as much code as possible using the Curiously Recurring Template Pattern. Here's the base code, which compiles in some cases, but not in others: template<typename T, size_t N, typename ConcreteClass> struct _BaseClassOperators { const ConcreteClass& operator+=(const ConcreteClass& other) { ConcreteClass& self = static_cast<ConcreteClass&>(*this); for(size_t i = 0; i < N; ++i) { self.data[i] += other.data[i]; } return self; } const

How do you copy a constexpr array to another constexpr array with std::copy?

狂风中的少年 提交于 2019-12-11 04:13:07
问题 In the following code, I create an array of length 6 and initialize it with 1, 2 and 3 in the first 3 elements. Then I copy the first 3 elements to the last 3 elements. Then I print all the elements in order. std::array<int, 6> bar = {1, 2, 3}; int main(){ // Copy the first 3 elements to the last 3 elements std::copy(bar.begin(), bar.end() - 3, bar.end() - 3); // Print all the elements of bar for(auto& i: bar) std::cout << i << std::endl; } It works fine, but when I try to make the array

enable_if using a constexpr bool test not working

那年仲夏 提交于 2019-12-11 03:24:00
问题 I have a maths function that I want to be able to accept either a double, or a array/vector/container of doubles, and behave slightly differently. I am attempting to use SFINAE and type traits to select the correct function. Here is a minimal example: #include <iostream> #include <vector> #include <type_traits> template <typename T> constexpr bool Iscontainer() { if constexpr (std::is_class<T>::value && std::is_arithmetic<typename T::value_type>::value) { return true; } return false; } //

Completely enumerate indices of D-dimensional array at compile time

对着背影说爱祢 提交于 2019-12-11 03:13:37
问题 To test some multidimensional structures there is a need to generate compile time multidimensional indices to fully cover all the possible cases. I search for compile-time inexpensive way to achieve above purpose. What I do currently: #include <type_traits> #include <utility> template< typename F, std::size_t ...indices > struct enumerator; template< typename F > struct enumerator< F > { constexpr enumerator(F && _f) : f(std::forward< F >(_f)) { ; } template< std::size_t ...I > constexpr bool

Access static constexpr std::array without out-of-class definition

懵懂的女人 提交于 2019-12-11 02:38:24
问题 I have a class that defines some arrays. Points.hpp class Points { public: static constexpr std::array< double, 1 > a1 = { { +0.0 } }; static constexpr std::array< double, 2 > a2 = { { -1.0 / std::sqrt( 3.0 ), +1.0 / std::sqrt( 3.0 ) } }; }; My main file then uses these arrays. main.cpp #include "Points.hpp" int main() { // Example on how to access a point. auto point = Points::a2[0]; // Do something with point. } When I compile my code, using C++11 and g++ 4.8.2, I get the following linker

constexpr won't work using Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)

喜欢而已 提交于 2019-12-10 23:51:51
问题 I'm trying to implement a compile-time hash algorithm using constexpr. I Downloaded Nov 2013 CTP because it has supported for constexpr but thats a lie... #define hashCharacter(T, J) (((T >> 0x0D) | (T << 0x13)) + J) unsigned long constexpr GetHashCompile(const char * asSource, unsigned long asValue = 0) { return asSource[0] == '\0' ? asValue : GetHashCompile(asSource + 1, hashCharacter(asValue, asSource[0])); } int main(int a, char ** b) { const auto value = GetHashCompile("Hello from

Get the size of multi dimensional std::array at compile time via constexpr or template function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 23:21:11
问题 I use a three dimensional std::array , because the size is already known at compile. However, I noticed that the size() function is not static, and thus, inaccessible for constexpr/template functions. I already found the demo example below, which estimates the size of a one dimensional std::array . However, this does not work for two or more dimensions. Is there a way to return the other dimensions by writing a function with an additional template parameter dim for the x, y, z, .. dimension?

constexpr function as array size

有些话、适合烂在心里 提交于 2019-12-10 21:09:46
问题 I'm trying to figure out why my code compiles, when it shouldn't: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; constexpr int ret_one() { return 1; } constexpr int f(int p) { return ret_one() * p; } int main() { int i = 2; srand(time(0)); int j = rand(); int first_array[f(10)]; // OK - 10 is a constant expression int second_array[f(j)]; // Error - the parameter is not a constant expression j = f(i); // OK - doesn't need to be constexpr std::cout << sizeof(second

Does the C++ standard explicitly disallow default arguments in calls through constexpr member function pointers?

旧巷老猫 提交于 2019-12-10 18:42:08
问题 Consider the code below: struct foo { int bar(int, int = 0) { return 0; } }; constexpr auto ptr = &foo::bar; int main() { return (foo{}.*ptr)(0); } As expected, this code fails to compile with recent versions of GCC, Clang, and MSVC. It is, however, plausible that a hypothetical compiler be capable of passing default arguments through a constexpr member function pointer. If this compiler successfully compiled the code above <edit> without warnings </edit> , effectively passing 0, 0 to foo: