decltype to declare parameter of return type of a function (without auto)

269 Views Asked by At

This looks like a trivial question for me, maybe I didn't find the right documentation..

I have a struct A, and I want to define parameter b to be of return type of function A:

struct A{
    int operator[](int);
};

and then at some point

decltype(A::operator[]) b = 0;

I could do this: but it's ugly..

A a;
decltype(a[0]) b = 0;

(it can be double / int etc), I don't want to use templates.

Thanks,

1

There are 1 best solutions below

2
On BEST ANSWER

I don't quite understand the need, other than playing with the syntax. That is precisely what auto was designed for, and auto is supported by the same standard that added decltype...

At any rate, you need to simulate the function call:

decltype(std::declval<A>()[0]) b = 0;   // equivalent to `int b = 0;`