constexpr question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I've been playing around with constexpr in gcc 4.6 and have a question:

This works:

template <int v> struct wrapper
   { enum : int { value = v }; };

constexpr int test1(int a)
   { return a; }

constexpr int test2(int a)
   { return test1(a); }

constexpr int one = 12;
constexpr int two = wrapper<test2(one)>::value;


But this doesn't work:

constexpr int test3(int a)
   { return wrapper<test1(a)>::value; }

constexpr int three = test3(one);


The compiler error is:

In function 'constexpr int test3(int)':
error: 'a' is not a constant expression
note: in template argument for type 'int'


The question I have is: why doesn't (or can't) the second case work?  In
function 'test2' the compiler believes 'a' is a constant expression and the
declaration of variable 'two' shows 'wrapper' can take a constexpr as its
template parameter, so is it just an oversight of the compiler that 'test3'
doesn't compile?  Or is it intentional?

I understand that test3 would not compile if passed a non-constexpr
parameter, whereas test2 would compile -- but this is what I would want:
this way I can stop test3 being used except during compile-time.  Or is
there a better way of doing this?

Thanks for any help!

Andy






[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux