r/cpp11 • u/YouFeedTheFish • Sep 12 '16
r/cpp11 • u/YouFeedTheFish • Aug 29 '13
Tuple Forwarding [xpost: r/cpp, credit to /u/meetingcpp]
r/cpp11 • u/YouFeedTheFish • Jun 29 '13
Non-Static Data Member Initializers
blogs.oracle.comr/cpp11 • u/YouFeedTheFish • Jun 26 '13
C++11 Regular-Expression Library | InformIT [x-post: /r/cpp; credit to /u/mttd]
r/cpp11 • u/YouFeedTheFish • Mar 05 '13
The importance of std::function [xpost from cpp]
r/cpp11 • u/YouFeedTheFish • Feb 05 '13
constexpr question
I was tinkering around with constexpr to solve another redditor's question in a novel way and I noticed that the compiler was generating less-than-optimal code, depending on how I was applying the statement. This code adds all of the even numbers up to 100.
In the first instance, the compiler did what I expected:
#include <iostream>
template<int NUM> class Adder{
public:
constexpr static int RUNNING_TOTAL = Adder<NUM-2>::RUNNING_TOTAL + NUM;
};
template<> class Adder<0>{
public:
constexpr static int RUNNING_TOTAL = 0;
};
int main(int argc, const char * argv[])
{
std::cout << Adder<100>::RUNNING_TOTAL<<std::endl;
return 0;
}And the compiler generated this (snippet):
movl $2550, %ecx Here, it rolled up all of the loops and output the sum. Great. Unfortunately, when I removed the classes and used functional template specialization, like so:
#include <iostream>
template<int NUM> constexpr int getTotal(){return getTotal<NUM-2>() + NUM;}
template<> constexpr int getTotal<0>(){return 0;}
int main(int argc, const char * argv[])
{
std::cout << getTotal<100>() <<std::endl;
return 0;
}I end up getting this (snippet):
callq __Z8getTotalILi100EEiv
movq __ZNSt3__14coutE@GOTPCREL(%rip), %rdi...
__Z8getTotalILi100EEiv:
callq __Z8getTotalILi98EEiv
addl $100, %eaxAnd this continues, one function calling another until the solution is reached at runtime.
Why isn't constexpr rolling up the loops in this case? Did I violate constexpr-ness somehow?
Weirdly, sometimes it does roll it up:
template<int NUM> constexpr int getTotal(){return getTotal<NUM-2>() + NUM;}
template<> constexpr int getTotal<0>(){return 0;}
template<int N>
class Foo{
public:
static constexpr int getNumber(){return N;}
};
int main(int argc, const char * argv[])
{
//std::cout << Adder<100>::RUNNING_TOTAL <<std::endl;
std::cout <<Foo<getTotal<14>()>::getNumber()<<std::endl;
return 0;
}Which gives me the following assembly:
__ZN3FooILi56EE9getNumberEv:
movl $56, %eax //<-- 56 is the right answer.
popq %rbp
ret[edit: Should note that I am using the Apple LLVM compiler 4.2.]
[edit 2: Incidentally, just typing getTotal<getTotal<3>()>() crashes my ide (xcode 4.6) consistently.]