c++-concepts

What's the relationship between C++ “concept” and duck typing?

风流意气都作罢 提交于 2021-02-18 22:10:28
问题 There was an earlier question (8 years ago!) about the relationship between templates and duck typing here: What's the relationship between C++ template and duck typing? I've borrowed and modified the tag line for my question on a new feature of C++. With C++20 there will be the new feature of "concept" that looks much more like a duck-typing feature. Is it correct that the new C++ "concept" is equivalent to duck typing for C++? If not, how is it different? 回答1: With C++20 there will be the

Disable non-templated methods with concepts

落花浮王杯 提交于 2021-02-16 17:58:25
问题 Is there a syntax to constraint a non-templated method? All the syntaxes I've tried on godbolt with clang concepts branch and gcc fail to compile: // these examples do not compile template <bool B> struct X { requires B void foo() {} }; template <class T> struct Y { requires (std::is_trivially_copyable_v<T>) auto foo() {} }; The trick to make it compile is the same trick you needed to do with SFINAE, make the methods template, even though they really are not templates. And funny enough, the

C++, concepts not working with unsigned integers as a result type?

时光怂恿深爱的人放手 提交于 2021-02-16 04:55:13
问题 I am playing around with concepts and I tried to define a concept which takes any non-type parameter and the function isUnsignedInt checks if the parameter is an unsigned int , using the required keyword followed by the concept . The problem is, that I can pass a negative integer and there is no error message, that the type is not unsigned int . Is my understanding of concepts wrong? Currently I am working with the gcc 9.2 compiler and my CMake contains add_compile_options(-fconcepts) , which

“requires” ignores a field is not static

我们两清 提交于 2021-02-08 19:47:11
问题 Consider the following code: #include <iostream> constexpr int fun(int const&) { return 5; } struct T { int x; }; int main() { std::cout << fun(T::x) << std::endl; // Line A std::cout << requires { fun(T::x); } << std::endl; // Line B } If I only comment Line B, then the code cannot be compiled (as expected, since x is not static in T ). But when I only comment Line A, the code compiles just fine with both Clang 11.0.0 and GCC 10.2.0 (both output 1 ). What it is that I am missing about

Visual Studio 2019 rejects `bool concept` while gcc 8 doesn't compile concepts without `bool`

删除回忆录丶 提交于 2021-02-08 09:58:06
问题 I thought c++ concepts is a better method to write c++ templated code with better error messages and faster compile times , so I upgraded Visual Studio to 2019 and still waiting for clang to support concepts however I tested some simple code with msvc from visual studio 2019 and g++ 8 from mingw-w64 and I'm having some trouble. This is the test: #include <iostream> using namespace std; // this compiles under g++ 8 but not visual studio 2019 template <class T> bool concept CharT = std::is_same

Idiomatic way to write concept that says that type is a std::vector

不羁的心 提交于 2021-02-07 20:39:14
问题 I have the following code that implements following type traits: that type is std::vector that type is std::vector of ints It works but it is quite verbose. Is there a shorter/nicer way to write this using concepts? I know I can steal concepts from range-v3 or some other similar library, but let's assume I want to implement it myself. #include <iostream> #include <string> #include <type_traits> #include <vector> template <class T> struct is_vector { static constexpr bool value = false; };

Could type traits be restricted to not accept other type traits as arguments?

泪湿孤枕 提交于 2021-02-07 07:17:22
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

Could type traits be restricted to not accept other type traits as arguments?

假装没事ソ 提交于 2021-02-07 07:14:52
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

Is UB in unevaluated context (e.g. requires-expressions) still UB?

半世苍凉 提交于 2021-02-07 05:23:24
问题 The C++ 20 draft [concept.default.init] does not precisely define default_initializable template<class T> concept default_initializable = constructible_from<T> && requires { T{}; } && is-default-initializable <T>; // exposition-only and describe what is-default-initializable should do with the following words: For a type T , is-default-initializable <T> is true if and only if the variable definition T t; is well-formed for some invented variable t; otherwise it is false. Access checking is

Constrained member functions and explicit template instantiation

泪湿孤枕 提交于 2021-02-07 01:21:45
问题 G++ and Clang++ agree that the following snippet is not valid C++: template<int dim, int rank> struct Tensor {}; template<int dim> double InnerProduct(Tensor<dim, 1> const &, Tensor<dim, 1> const &) { return 0.0; } template<int dim> double DoubleInnerProduct(Tensor<dim, 2> const &, Tensor<dim, 2> const &) { return 0.0; } template<int dim, int rank> class Field { private: static double Dot(Tensor<dim, rank> const &u, Tensor<dim, rank> const &v) requires (rank == 1) { return InnerProduct(u, v);