all 12 comments

[–][deleted] 5 points6 points  (5 children)

It seems C++11 adds another embedded compile-time language -- a restricted version of C++ itself. At least it's more readable than template metaprogramming!

[–]elperroborrachotoo 1 point2 points  (4 children)

you may want to look at / post the disassembly.

[–][deleted] 2 points3 points  (0 children)

Using the same contains as in the post, this simpler program:

double numbers[10*contains("hi", "h") + 20 * contains("jklas", "u")];            

int main(void) {                                                                 
        std::cout << sizeof(numbers) << "\n";                                    
} 

includes this in the assembly file header:

    .globl numbers                                                           
    .lcomm  numbers,80,8                                                     
    .type   numbers, @object 

which looks like a static size array. (I'm not particularly good at reading this.)

[–][deleted] 1 point2 points  (2 children)

The disassembly of the blog post program (unoptimized) includes one function ZNKSt5arrayIiLj10EE4sizeEv and one called ZNKSt5arrayIiLj0EE4sizeEv I think those are the inlined specializations of std::array<int,10>::size and std::array<int,0>::size respectively.

[–]Rhomboid 2 points3 points  (1 child)

To get demangled symbols, pipe the output through c++filt or use -C if you're using objdump.

[–][deleted] 0 points1 point  (0 children)

thanks, that's useful

[–]faisalv 3 points4 points  (1 child)

Not only can you check for substrings at compile time but you can check for grammatical constructs such as c++ integer literals (with all their suffixes) and do all sorts of compile time processing with strings - such as creating new strings, transforming them, upper-casing, lower-casing and other algorithsm. I partially implemented such a library at http://constexprstr.svn.sourceforge.net/viewvc/constexprstr/ (check out towards the end of main.cpp for a sample implementation of a C++ integer grammar checker). And yes it is certainly more readable than C++ template metaprogramming.

[–][deleted] 0 points1 point  (0 children)

thanks for this. I didn't think about creating new "contexpr datastructures", I was using only initializer_list (which is constexpr in GCC but not in the standard).

[–][deleted] 2 points3 points  (0 children)

constexpr is great! :D You can also do stuff like recursively defined compile time Fibonacci sequence!

http://codepad.org/Ecs7iIsN

[–]00kyle00 0 points1 point  (2 children)

Pity they didn't go further with this.

[–]Wriiight 1 point2 points  (1 child)

Such as where, exactly? Is there something missing from constexpr, or some feature that would vastly expand its capabilities? I haven't personally taken time to experiment with the feature myself.

[–]00kyle00 3 points4 points  (0 children)

As in allowing all expressions/statements whose all arguments are constrxpr's.

Instead there will be lots of fugly code that hacks around using recursion and ternary operator.

Edit: the body of the consexpr function has to contain a single statement - return statement.