Alon
Alon

Reputation: 1804

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

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,

Upvotes: 0

Views: 277

Answers (1)

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;`

Upvotes: 2

Related Questions