constexpr

Constexpr class: Inheritance?

我怕爱的太早我们不能终老 提交于 2019-12-13 14:43:24
问题 First of all, I'm working with Clang 3.4.1 I'm writting a global variable which has to serve as a placeholder on compile-time contexts (Primarily as value template parameter). For that purpose, I have written a constexpr class named chameleon (It mimics the behaviour of any runtime value): struct chameleon { template<typename T> constexpr operator T() const { return T{}; } constexpr chameleon() = default; }; Since both the conversion operator and the constructor are specified as constexpr , I

if vs if constexpr inside constexpr function

可紊 提交于 2019-12-13 14:21:12
问题 Recently I modify some if constexpr into if in my constexpr functions and found they still work fine and can be evaluated when compile time. Here is a minimum case: template<int N> constexpr bool is_negative() { if constexpr (N >= 0) return false; else return true; } int main() { constexpr bool v = is_negative<1>(); } live demo In the case above, N must be known at compile time because it is non-type template parameter, so if constexpr works fine here. However, it is a constexpr function, so,

Why isn't constexpr implied when applicable?

大憨熊 提交于 2019-12-13 11:38:32
问题 These should probably be in different questions, but they're related so... Why do we need to write constexpr at all? Given a set of restrictions couldn't a compiler evaluate code to see if it satisfies the constexpr requirements, and treat it as constexpr if it does? As a purely documentation keyword I'm not sure it holds up because I can't think of a case where I (the user of someone else's constexpr function) should really care if it's run time or not. Here's my logic: If it's an expensive

How to specify type of a constexpr function returning a class (without resorting to auto keyword)

a 夏天 提交于 2019-12-13 09:56:06
问题 Basically in below I want to see if I can get around having to use auto keyword Suppose that we have the following piece of code [works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) & clang version 3.6.0] : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } How can I specify the type of

Nested static loop fails to work due to constexpr uncapturable

别说谁变了你拦得住时间么 提交于 2019-12-13 09:33:21
问题 I have this static_loop construct that is used for type dispatching over loop unrolling. template <std::size_t n, typename F> void static_loop(F&& f) { static_assert(n <= 8 && "static loop size should <= 8"); if constexpr (n >= 8) f(std::integral_constant<size_t, n - 8>()); if constexpr (n >= 7) f(std::integral_constant<size_t, n - 7>()); if constexpr (n >= 6) f(std::integral_constant<size_t, n - 6>()); if constexpr (n >= 5) f(std::integral_constant<size_t, n - 5>()); if constexpr (n >= 4) f

C++ - Static_assert and ability of constexpr functions to evaluate at runtime

拟墨画扇 提交于 2019-12-13 08:11:10
问题 I'm reading about constexpr and static_assert features in C++ and one thing seems confusing to me - I've read that constexpr functions are not necessarily always evaluated during compilation and they can sometimes evaluate at runtime. One thing that bothers me is that static_assert is always checked during compilation. So what happens, if we pass constexpr to static_assert , but compiler chooses to evaluate that constexpr during runtime? Is that even an issue? 回答1: constexpr functions are not

Enum with constexpr std::string_view vs instantiated enum with std::string

房东的猫 提交于 2019-12-13 00:49:46
问题 I would like to have struct s holding enum s for iteration, together with std::string holding their names for creating menu entries. I was using something like this: struct S { enum E { ONE, TWO, ALL }; std::array<std::string, ALL> names { "one", "two" }; }; int main() { S s; for(int i=s.ONE; i<s.ALL; ++i) // or S::... std::cout << s.names[i] << '\n'; return 0; } As I understand, this is preferred to having global variables. It works, but needs instantiation before usage. Now, I found out

Assign a const to a constexpr variable

拈花ヽ惹草 提交于 2019-12-12 22:23:04
问题 I tried to run a program based on constexpr . Code:- #include <iostream> using namespace std; int main() { const int i = 10; constexpr int j = 10; constexpr int val1 = i; constexpr int val2 = j; return 0; } In the book I follow, it is mentioned that if you assign a const to a constexpr variable, it is an error. But my program compiles without any complaints. Am I missing something? 回答1: Ammendment celtschk made a good point in the comments below the question. That is, you are not assigning to

Linker error (undefined reference) with `static constexpr const char*` and perfect-forwarding [duplicate]

£可爱£侵袭症+ 提交于 2019-12-12 16:36:00
问题 This question already has answers here : Undefined reference to static constexpr char[] (6 answers) Closed 4 years ago . #include <iostream> using namespace std; template<typename T> void print(T&& mX) { std::cout << std::forward<T>(mX) << std::endl; } struct SomeStruct { static constexpr const char* someString{"hello!"}; SomeStruct() { print(someString); } }; int main() { SomeStruct s{}; return 0; } clang++ -std=c++1y ./code.cpp -o code.o /tmp/code-a049fe.o: In function `SomeStruct:

What's the most efficient tail recursive prime verification function known?

那年仲夏 提交于 2019-12-12 13:19:52
问题 I was experimenting with meta programming to this point: // compiled on Ubuntu 13.04 with: // clang++ -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes // assembly output with: // clang++ -S -mllvm --x86-asm-syntax=intel -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes.asm #include <array> #include <iostream> template