How to avoid implicit conversion in C++ concepts?

3 weeks ago 19
ARTICLE AD BOX

Is there a way to prevent implicit conversions happening in concepts? For example, this example compiles with GCC and clang when I actually want it to give an error that the char constructor is missing:

template<typename T> concept specific_constructors = requires { T(int{}, char{}); T(int{}); T(char{}); }; struct test { test(int a_, char b_): a{a_}, b{b_} {} explicit test(int a_): a{a_}, b{} {} //explicit test(char b_): a{}, b{b_} {} private: int a; char b; }; auto main() -> int { specific_constructors auto const v1 {test{1,'a'}}; }

Currently, the things that I've thought of are just wrapping the char in a struct, or using a strong type, but I am looking for any other options available.


EDIT:

Just for some clarity, the question is how do I write the concept specific_constructors so that it fails for class test, because it doesn't specifically have a char constructor.

Remy Lebeau's user avatar

Remy Lebeau

610k36 gold badges516 silver badges875 bronze badges

Hello Worlder's user avatar

New contributor

Hello Worlder is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

8

Read Entire Article