Interesting template usage

Hi everyone

I was reading articles and questions people asked at internet about static_assert, so that i can learn about it.

I came across this question, that is asked by PorkyBrain
http://stackoverflow.com/questions/17577749/why-is-comparing-two-parameters-of-a-constexpr-function-not-a-constant-condition

Q1 : Something took my attention there. Guy uses template specifically for int, and not for a generic type. I couldnt find an example usage like that on internet.

Q2 : template<int i,int Size> in function parameters guy did not passed 'i', but used it inside function definition.

Q3 : If that function is working how can i use that function? Like
BitPositionToMask(); ??? where do i add 'i' integer as an argument.

My questions could be silly, and sorry if it is. I just got confused by this.

Thanks in advance
Template metaprogramming is actually older than the C++ standard.

Example: http://cpptruths.blogspot.fi/2011/07/want-speed-use-constexpr-meta.html

These are legal:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <int N>
void foo() {
  std::cout << N;
}

template <typename T>
void bar( int x ) {
  std::cout << static_cast<T>( x );;
}

// use
foo<42>();
bar<double>( 7 );

(Earlier MSVC did not support the syntax and one was forced to add dummy parameters to help the template type deduction.)
Wow, pretty interesting. Thank you for the answer.

Going to try it out today.

EDIT : Tried it out and it is awesome.
Last edited on
Topic archived. No new replies allowed.