How to obtain a const qualified declval?
c++, c++11, constants, decltype
Solution
GCC 4.8.1 likes this:
template <typename T = decltype(std::declval<Base const>().f())>
You need to tell the compiler that the object through which you make the call is `const`.
You cannot specify `Derived` here in this `declval` because at the point of the call, `Derived` is an incomplete type. In addition, you don't even need `Derived` as part of the `declval` since `f()` is a member of `Base`.
Problem
Consider the following code: ``` #include <iostream> #include <type_traits> #include <typeinfo> struct Base { int f() const; double f(); }; struct Derived : public Base { template <typename T = decltype(std::declval<Derived>().f())> // Modify this T g() const; }; int main() { const Derived x; std::cout<<typeid(decltype(x.g())).name()<<std::endl; // Prints "d", not "i" return 0; } ``` How to modify `decltype(std::declval<Derived>().f())` so that it will return `int` and not `double`? I've tried `decltype(std::declval<const Derived>().f()` but it does not compile.