1

Is it possible to do something like:

template< typename T >
using T_CCUnit = static const constexpr T;

In fact i just want an alias for static const constexpr, because it's really boring to write everytime in some file. I'll prefer a solution in c++11.

Barry
  • 247,587
  • 26
  • 487
  • 819
Mathieu Van Nevel
  • 1,352
  • 1
  • 8
  • 23

4 Answers4

4

You can add const-ness to the type easily enough, because cv-qualifiers modify the type:

template <typename T>
using T_C = std::add_const_t<T>;

However, static and constexpr modify declarations, rather than types. So, here:

                         int x1;
                   const int x2;
  static           const int x3;
  static constexpr const int x4;
//      |    ^    |  type   |name
//   ^  | constexpr specifier
// storage class specifier

the last three declarations are all of type const int, but x2 has different storage class to x3 and x4.

Honestly, if the main problem is that

... it's really boring to write ...

my best suggestion is that you learn how to write macros or save common code snippets in your preferred editor.

Useless
  • 55,472
  • 5
  • 73
  • 117
  • I'll stay on macro like teivaz said. Thanks for the details! Snippets won't do it, because i'm making a lot of flag declaration, and it will be a mess with all these keywords. – Mathieu Van Nevel Apr 14 '16 at 18:03
0

I think the best solution for you will be to use macro for that.

#define STATIC_CONST_CONSTEXPR static const constexpr

But seriously, static and constexpr are not modifiers for the type. All you can do with using in your case is const T. Everything else you either write yourself or ask preprocessor to write for you.

Teivaz
  • 4,616
  • 4
  • 25
  • 63
  • Yes, you're right. I was so focus to found a "c++" method that i forgot the simpliest one : macro. I'll wait a little if somebody have another idea. – Mathieu Van Nevel Apr 14 '16 at 14:15
0

The static storage qualifier applies to instances, not types. So it's impossible to typedef it.

Tamás Zahola
  • 8,822
  • 4
  • 28
  • 44
0

You cannot typedef static or constexpr - neither are part of the type. That said, even if you could, nobody will know what:

T_CCUnit<int> x = 4;

means, but everybody will know what:

static constexpr int x = 4;

means. It is worth typing an extra 7 characters every time to make your code more legible. All readers of your code, and your future self, will thank you.


Side-note, const is redundant with constexpr - every variable declared constexpr is implicitly const.

Barry
  • 247,587
  • 26
  • 487
  • 819
  • I know i spoke about c++11, but constexpr is no more const in c++14. So if i can move on c++14, i'll have some problem. – Mathieu Van Nevel Apr 14 '16 at 18:05
  • @MathieuVanNevel "A `constexpr` specifier used in an object declaration declares the object as `const`." [\[dcl.constexpr\]](http://eel.is/c++draft/dcl.constexpr#9) – Barry Apr 14 '16 at 18:13
  • Oh seems you're right about static member, the clause speak only about non-static member. A little strange but why not, i'll try it, thanks. – Mathieu Van Nevel Apr 14 '16 at 18:36
  • @MathieuVanNevel The phrase I cited speaks about objects, not about members. Non-static data members can't be `constexpr`. – Barry Apr 14 '16 at 19:00
  • Sorry i missed a word in my comment, the clause speak about functions. I need to check more about c++14... – Mathieu Van Nevel Apr 14 '16 at 19:18
  • @MathieuVanNevel No, the paragraph cited is about objects, not functions. – Barry Apr 14 '16 at 19:32
  • Yes that's why i said you're right. The clause i remembered was only for non-static function member. – Mathieu Van Nevel Apr 14 '16 at 19:36