Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Determining the return type of a class method without warning on gcc

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading