Question

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,

Was it helpful?

Solution

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;`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top