Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ilya-biryukov opened this issue Jan 9, 2025 · 1 comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@ilya-biryukov
Copy link
Contributor

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.

@ilya-biryukov ilya-biryukov added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jan 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/issue-subscribers-clang-frontend

Author: Ilya Biryukov (ilya-biryukov)

The following code fails to compile (see https://godbolt.org/z/zTf49f9oe): ```cpp 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):
```cpp
    auto example = Example&lt;&gt;();

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

2 participants