c++-concepts

Call less constrained functionally equivalent function

安稳与你 提交于 2021-02-04 16:23:07
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Call less constrained functionally equivalent function

非 Y 不嫁゛ 提交于 2021-02-04 16:22:50
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Let ss be a std::stringstream. How to specify a concept for (ss<<some_type).str() is a std::string?

孤人 提交于 2021-01-28 00:33:55
问题 Let ss be a std::stringstream. How to specify a concept for (ss << some_type).str() is a std::string? Here is the code I have so far: #include <iostream> #include <sstream> namespace detail { template< class T, class U > concept SameHelper = std::is_same_v<T, U>; } template< class T, class U > concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>; template<typename T> concept ssToStr = requires(T a, std::stringstream ss) { { (ss << a).str() } -> same_as<std::string>; }; void

C++20 Concepts: out-of-line-definition fails with MSVC but not in GCC or clang

好久不见. 提交于 2021-01-27 19:54:51
问题 Consider this little code snippet namespace nsp { template<typename T> concept Addable= requires(const T& a,const T& b) { {a + b} -> std::convertible_to<T>; }; template<Addable T> struct C { C(); }; } template<nsp::Addable T> nsp::C<T>::C() {}; As shown here GCC (10.2) and clang (11.0) accept the code while MSVC (x86 19.28) rejects it with the error message: error C3855: 'nsp::C<T>': template parameter 'T' is incompatible with the declaration. Is this a MSVC bug or are GCC and clang wrong

visual studio 2019 c++ - concept identifier is undefined

元气小坏坏 提交于 2021-01-27 12:10:35
问题 I have tried to use concepts in my cpp project which I write using visual studio 2019 version 16.4.1 but I get and error message: "identifier conecpt is undefined". As far as I know concepts have support in visual studio 2019 since version 16.3 so I cant understand why it still doesn't work for me. Here is the code I have written, Is there any foolish syntax error in it? #include<concepts> template<typename T> concept has_type_member = requires { typename T::type; } If not has anyone have had

Does a class template's requires clause have to be repeated outside member definitions?

只谈情不闲聊 提交于 2021-01-27 06:59:29
问题 When the member of a class template that uses the requires clause is defined outside the class, gcc does not complain if requires is not specified, whereas clang does. Consider the code snippet below: #include <concepts> template<typename Container> requires std::integral<typename Container::value_type> class Foo { public: void func(); }; template<typename Container> void Foo<Container>::func() {} The compilation using gcc does not complain. While clang reports the following error: ❯ clang++

Does a class template's requires clause have to be repeated outside member definitions?

你离开我真会死。 提交于 2021-01-27 06:57:26
问题 When the member of a class template that uses the requires clause is defined outside the class, gcc does not complain if requires is not specified, whereas clang does. Consider the code snippet below: #include <concepts> template<typename Container> requires std::integral<typename Container::value_type> class Foo { public: void func(); }; template<typename Container> void Foo<Container>::func() {} The compilation using gcc does not complain. While clang reports the following error: ❯ clang++

SFINAE inside concept template argument

我的梦境 提交于 2021-01-27 06:29:08
问题 Does SFINAE work inside a concept argument? (maybe it's not called SFINAE here). Example: template <class F> requires std::invocable<F, int> && // <-- is this needed? (!std::same_as<std::invoke_result_t<F, int>, void>) auto foo(F f, int a) -> int Is the above std::invocable<F, int> required? If we omit it like so: template <class F> requires (!std::same_as<std::invoke_result_t<F, int>, void>) auto foo(F f, int a) -> int Is this version well-formed even if std::invoke_result_t<F, int> is not

How does the placement of a concept definition change program behaviour

三世轮回 提交于 2021-01-03 08:23:50
问题 I'm compiling this code with gcc 9.3 with -fconcepts . The following compiles successfully void f(int) {} // 1 template<typename T> // 2 concept C = requires (T a) { { f(a) }; }; template<C T> // 3 void g() { f(42); } int main() { g<int>(); } // 4 However, if I define the function f after I define the concept C , template<typename T> // 2 concept C = requires (T a) { { f(a) }; }; void f(int) {} // 1 template<C T> // 3 void g() { f(42); } int main() { g<int>(); } // 4 then the program fails to

How does the placement of a concept definition change program behaviour

北城以北 提交于 2021-01-03 08:21:36
问题 I'm compiling this code with gcc 9.3 with -fconcepts . The following compiles successfully void f(int) {} // 1 template<typename T> // 2 concept C = requires (T a) { { f(a) }; }; template<C T> // 3 void g() { f(42); } int main() { g<int>(); } // 4 However, if I define the function f after I define the concept C , template<typename T> // 2 concept C = requires (T a) { { f(a) }; }; void f(int) {} // 1 template<C T> // 3 void g() { f(42); } int main() { g<int>(); } // 4 then the program fails to