4

I was watching Bjarne Stroustrup's talk "The Essential C++".

In Q&A session, on how to manage heavy template programming codes, he mentioned that: "by constack perfunction you can eliminate essentially every template metaprogramming that generates a value by writting ordinary code".

The constack perfunction is just a wild guess by the sound.

May I ask what is the correct term for that technology? so that I could do some follow up reading.

update: just modify the title to "constexpr function".

athos
  • 6,120
  • 5
  • 51
  • 95
  • 4
    `constexpr` function? – tmlen Jan 01 '15 at 04:37
  • Please see if you can get transcript of his talk.. you are adding too many questions from same talk in a row!! man. – Pranit Kothari Jan 01 '15 at 04:38
  • 4
    Probably constexpr function, I give an example of how to replace template meta-programming with constexpr functions in my [answer here](http://stackoverflow.com/a/21519186/1708801). My example comes from [Want speed? Use constexpr meta-programming!](http://cpptruths.blogspot.com/2011/07/want-speed-use-constexpr-meta.html) which is a good article on the topic. – Shafik Yaghmour Jan 01 '15 at 04:38
  • constack perfunction: (v) An upside-down stack (constack) for each function (perfunction). Also, not actual words. – Yakk - Adam Nevraumont Jan 01 '15 at 04:40
  • That's over an hour and a half - did you want us to listen to the entire thing for your single sentence?! – dwn Jan 01 '15 at 04:42
  • @PranitKothari let's say it's a stream flush :p i do a batch job, list the things i don't understand, then try to figure it out by my own, then the rest, seek help... – athos Jan 01 '15 at 04:51

1 Answers1

7

constexpr functions, added in C++11, can be evaluated at compile-time and be used as template arguments in template metaprogramming. In C++11 they are very limited and can (nearly) only consist of a single return expression. C++14 makes them less restrictive.

For example this is possible:

constexpr std::size_t twice(std::size_t sz) {
    return 2 * sz;
}

std::array<int, twice(5)> array;

Whereas before C++11, template 'hacks' were needed, like for instance:

template<std::size_t sz>
class twice {
public:
    static const std::size_t value = 2 * sz;
}

std::array<int, twice<5>::value> array;

It can for instance be used to generate values (like math constants, trigonometric lookup tables, ...) at compile-time in a clean manner.

tmlen
  • 8,533
  • 5
  • 31
  • 84