Home > Back-end >  Determining the return type of a class method without warning on gcc
Determining the return type of a class method without warning on gcc

Time:11-14

I would like to alias a return type for a templated class method, on clang I use

template <class R, unsigned int BlockSize>
struct block {
    using KeyType = decltype(((R*)nullptr)->getKey());
}

This works fine on clang, but on gcc 11.3.0 I get a warning:

 warning: ‘this’ pointer is null [-Wnonnull]

My question is what is the proper way to get the KeyType without warning?

CodePudding user response:

To be able to use member functions in unevaluated contexts such as decltype expressions one should use std::declval:

template <class R, unsigned int BlockSize>
struct block {
    using KeyType = decltype(std::declval<R>().getKey());
}
  • Related