Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Computing return type for auto fold-expressions

Hi Marco,

The approach I would suggest is to construct an evaluation tree whose structure corresponds to the expression tree.

In your example:

> template<typename... Args>
> auto sum_fold(Args... args) { return (... + args); }
> 
> struct T1 {}t1; struct T2 {}t2; struct T3 {}t3; struct T4 {}t4; struct T5 {}t5;
> 
> T3 operator+(T1, T2) { return t3; } T5 operator+(T3, T4) { return t5; }
> 
> int main() {
> auto foo = sum_fold(t1,t2,t4); // return type is T5
> }

You have evaluations for the concrete arguments t1, t2, and t4. I'll just refer to those as "t1", "t2", and "t4".

The expression that gets instantiated is "t1 + t2 + t4". Since + is left-associative, that's parenthesized as (t1 + t2) + t4.

So construct the following evaluation tree:

  EvalBinary
    left: EvalBinary
      left: t1
      right: t2
    right: t4

Then just call getType() on its root and you have your desired type. Things like considering overloading operators will be taken care of by the existing code in EvalBinary.

Hope that helps,
Nate

Back to the top