Skip to content

Incorrect "invalid conversion" error with decltype(<lambda>) in default template arguments #122287

Open
@ilya-biryukov

Description

@ilya-biryukov

The following code fails to compile (see https://godbolt.org/z/zTf49f9oe):

template <typename MakeValue = decltype([] { return 1; })>
class Example {
   public:
    using Result = decltype(MakeValue()());
    explicit Example(MakeValue&& make_value = {})
        : make_value_(make_value) {}

    Result Make() { return make_value_(); }

   private:
     MakeValue make_value_;
};

template <typename CastTo = int>
CastTo CastResultTo() {
    auto example = Example();
    return example.Make(); // error: 'Cannot initialize object parameter'
}

int main() { return CastResultTo(); }

Adding an empty template argument list makes the error go away (see https://godbolt.org/z/dz7eMaM54):

    auto example = Example<>();

So this seems to be related to deduction guides somehow. Could it be that we synthesize a new lambda type for CTAD and happen to mix it with types that we also creates during substitution?

cc @hokein who recently worked on CTAD, in case he has any ideas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"lambdaC++11 lambda expressions

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions