jeudi 23 juin 2016

Is it possible to define type alias to constexpr function

In C++11 or C++14, I'm trying to define a type alias to a constexpr function.

I tried:

#include <iostream>
constexpr int foo(int i, int j) { return i + j; }
using TConstExprFunction  = constexpr int (*)(int i, int j);

int main() {
  TConstExprFunction f = foo;
  constexpr int i = f(1, 2);
  std::cout << i << std::endl;
}

But it fails to compile with g++ and clang++.

g++: error: expected type-specifier before 'constexpr'

clang++: error: type name does not allow constexpr specifier to be specified

I have to do as below to make it compile

#include <iostream>
constexpr int foo(int i, int j) { return i + j; }
using TConstExprFunction  = int (*)(int i, int j);

int main() {
  constexpr TConstExprFunction f = foo;
  constexpr int i = f(1, 2);
  std::cout << i << std::endl;
}

From clang++'s error message, it seems I can NOT using constexpr for type name.

So, is it possible to define a type alias to a constexpr function; If yes, how?

Aucun commentaire:

Enregistrer un commentaire