Skip to content

[clang] constraints on constructors appear to be evaluated on class synthesis rather than on use #146246

Open
@ojhunt

Description

@ojhunt

I was confused about what was happening with https://discourse.llvm.org/t/detect-if-type-is-complete-not-forward-decl/87107

It looks like we're trying to evaluate requires constraints on constructor methods during class synthesis rather than on use of the constructor, but I believe evaluation of these constraints should not occur until synthesis of the constructor.

https://godbolt.org/z/eM5dx99sY

#include <type_traits>

struct Dummy;
template<class T> struct MyTemplate {
	T*p;
	MyTemplate() 
		requires (std::is_default_constructible_v<T>)
		{ }
};

class MyTest {
	MyTemplate<struct Dummy> data;
public:
	MyTest();
	void someFn() { }
};

MyTest test;

int main() {
	test.someFn();
}

struct Dummy {  int a; };
MyTest::MyTest() { }

This happens for all the constructors, even those that are not used - a minor modification: https://godbolt.org/z/YTK9c6adj - shows that the requires clause is evaluated for all constructors, even those that are not used in the TU:

#include <type_traits>

struct Dummy;
template<class T> struct MyTemplate {
	T*p;
    MyTemplate();
	MyTemplate(float f) 
		requires (std::is_default_constructible_v<T>)
		{ }
};

class MyTest {
	MyTemplate<struct Dummy> data;
public:
	MyTest();
	void someFn() { }
};

MyTest test;

int main() {
	test.someFn();
}

struct Dummy {  int a; };
MyTest::MyTest() { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"conceptsC++20 concepts

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions