Question Details

No question body available.

Tags

c++ c++20 c++-concepts

Answers (2)

Accepted Answer Available
Accepted Answer
December 31, 2025 Score: 14 Rep: 228,909 Quality: Expert Completeness: 70%

As it can only have one implicit conversion during a call, you might modify your concept that way:

template 
struct ExactType
{
    template 
    operator U() const requires (std::issamev);
};

template concept specific_constructors = requires { T(int{}, char{}); T(int{}); T(char{});

T(ExactType{}, ExactType{}); T(ExactType{}); T(ExactType{}); };

Demo

There are still some edge cases not handled, but should be ok for no-evil cases.

December 31, 2025 Score: 6 Rep: 48,070 Quality: High Completeness: 80%

Is there a way to prevent implicit conversions happening in concepts.

Yes, there is. You can explicitly =delete the char parameter ctor as shown below:

struct test {
    test(int a, char b): a{a}, b{b} {}
    explicit test(int a): a{a}, b{} {}
     test(char b) = delete;  //deleted it 
private:
    int a;
    char b;
};

Working demo. Now, we get the error you want.


It is possible to generalise this and provide a templated ctor that will only match the exact type of the function parameter. This can be done by changing test(char b) = delete to as shown below:


struct test {
    test(int a, char b): a{a}, b{b} {}
    explicit test(int a): a{a}, b{} {}
   template  test(T) = delete; //templated ctor
private:
    int a;
    char b;
};

Demo. Here the type T will be deduced using argument deduction.