Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decltype(fun()) for consteval methods

Consider the following code. I can compile it with GCC 10.2.0 and Clang 11.0.0 (as expected):

#include <iostream>

template<int> 
struct T {
  static constexpr auto fun() noexcept { return 0; }
  using type = std::remove_cvref_t<decltype(fun())>; 
};

int main() {
    decltype(T<1>::fun()) a = 1;
    std::cout << a; 
}

If I replace constexpr with consteval, then Clang complains about std::remove_cvref_t<decltype(fun())>:

error: cannot take address of consteval function 'fun' outside of an immediate invocation

GCC compile it just fine. Why?

like image 696
Koosha Avatar asked Nov 10 '20 03:11

Koosha


People also ask

What is consteval in c++?

The consteval specifier declares a function or function template to be an immediate function, that is, every potentially evaluated call (i.e. call out of an unevaluated context) to the function must (directly or indirectly) produce a compile time constant expression.

Is Decltype evaluated?

Like the sizeof operator, decltype 's operand is not evaluated.


1 Answers

As already said in comments to question, this is CLang bug.

This bug appears only if a function is a static method, if it is a global function then code works (see working online example here).

Hence one way to fix this is to use global function to forward result of static method. I did a bit more advanced example of such global forwarding below.

Try it online!

#include <iostream>
#include <type_traits>

template <typename T, typename ... Args>
static consteval auto forward_fun(Args ... args) {
    return T::fun(args...);
}

template <int I> 
struct T {
    static consteval auto fun(int i, bool f) noexcept {
        return i + 1;
    }
    using type = std::remove_cvref_t<decltype(forward_fun<T>(123, true))>;
};

int main() {
    T<1>::type a = 1;
    std::cout << a; 
    return 0;
}
like image 125
Arty Avatar answered Sep 30 '22 08:09

Arty