c++-concepts

C++ concept with friend-like access

廉价感情. 提交于 2019-12-19 17:13:10
问题 Is it possible to make this code work as I'd like? I.e. to allow the concept to have access to a private member funcion? template <typename T> concept bool Writeable() { return requires (T x,std::ostream os) { { x.Write(os) } -> void }; } template <Writeable T> void Write(std::ostream &os,const T &x) { x.Write(os); } class TT { private: void Write(std::ostream &os) const { os << "foo"; } //friend concept bool Writeable<TT>(); friend void ::Write<TT>(std::ostream &,const TT &); }; Thanks 回答1:

What are concepts?

北城余情 提交于 2019-12-18 11:45:01
问题 I've heard all this new (on /.) about C++0x not having concepts anymore, but I have no idea what they are? Can someone explain to me? 回答1: Concepts are a generic programming feature which allow someone writing templated code to specify requirements which the type parameters need to meet. For example, some collection types need for the type parameter for the collection to define the < operator. So the programmer might define a concept called LessThanComparable which tells the compiler that the

What's the difference between C++0x concepts and The Boost Concept Check Library (BCCL)?

China☆狼群 提交于 2019-12-18 11:06:38
问题 Concepts didn't make the C++0x standard, but Boost still provides The Boost Concept Check Library (BCCL). I guess that BCCL doesn't cover everything that was meant to into the C++0x standard. What is the difference between BCCL and the proposed C++0x solution? 回答1: Checking the template definition A big difference of concepts to these manual solutions is that concepts allow the definition of a template to be type-checked without doing anything special. The concept check library allows only

Are there any predefined concepts in Concepts TS?

心不动则不痛 提交于 2019-12-13 14:22:49
问题 'Concepts lite' were already accepted as a TS and (example implementation) merged into GCC main branch, so the follow up question is will any concepts come predefined (like Sortable or Random_access_range )? Where do I look for such predefined concepts? Is the list at cppreference.com an acurate and exhaustive list? Can I use them with the latest GCC trunk build? Edit 1 : Changed C++17 to TS due to concepts not being accepted into C++17. 回答1: There are no concepts defined in the Concepts TS

Can a concept evaluation depend on where it is evaluated?

廉价感情. 提交于 2019-12-12 07:46:56
问题 [temp.concept]/5 says: A concept is not instantiated ([temp.spec]). [ Note: An id-expression that denotes a concept specialization is evaluated as an expression ([expr.prim.id]). [...]] Does it mean that this rule bellow ([temp.point]/8) does not apply? If two different points of instantiation give a template specialization different meanings according to the one-definition rule, the program is ill-formed, no diagnostic required. For example if this rule does not apply, this code bellow is

Why use std::forward in concepts?

那年仲夏 提交于 2019-12-12 07:31:46
问题 I was reading the cppreference page on Constraints and noticed this example: // example constraint from the standard library (ranges TS) template <class T, class U = T> concept bool Swappable = requires(T t, U u) { swap(std::forward<T>(t), std::forward<U>(u)); swap(std::forward<U>(u), std::forward<T>(t)); }; I'm puzzled why they're using std::forward . Some attempt to support reference types in the template parameters? Don't we want to call swap with lvalues, and wouldn't the forward

concept checker doesn't compile on gcc because it 'has no linkage'

冷暖自知 提交于 2019-12-12 03:28:50
问题 I've created a concept checking class based on this question whose purpose is to make sure a given class has a static member function called baseUnitConversionFactor . The class compiles and works fine with msvc2013, but it wont compile on gcc 4.9.2 (using -std=c++14) with the error: error: ‘{anonymous}::UnitsTest_conceptChecker_Test::TestBody()::validUnit::baseUnitConversionFactor’ is not a valid template argument for type ‘double (*)()’ because ‘static double {anonymous}::UnitsTest

requires constraint must evaluate to bool. so no SFINAE

守給你的承諾、 提交于 2019-12-11 06:03:43
问题 I'm curious about the chapter "atomic constraints" https://en.cppreference.com/w/cpp/language/constraints it says The type of E after substitution must be exactly bool. No conversion is permitted and f(0); // error: S<int>{} does not have type bool when checking #1, // even though #2 is a better match ouch. which means there is no SFINAE mecanism when working with require clauses ? Isn't it a bummer ? Because I can see how some template types can result in bool after going through the

Will template constraints be available for variable templates?

戏子无情 提交于 2019-12-10 22:27:31
问题 In the latest template constraints paper a new toolset to constrain template arguments is presented. Also, in C++14 variable templates are provided. Variable templates allow the definition of type parameterized constants among other things. There is no mention of how these feature could interact. Using the canonical example of pi we could have this: template<Integral T> constexpr double pi(3.141592653589793238); template<Floating_point T> constexpr T pi(3.1415926535897932384626433832795029L);

Why does putting concept to type specifier fail type deduction when the same constraint must deduce different types?

白昼怎懂夜的黑 提交于 2019-12-10 17:34:39
问题 We have: template <typename ...T> concept bool Numerics = ( std::is_arithmetic_v<T> && ... ) ; template <typename T> concept bool Numeric = std::is_arithmetic_v<T>; Then we let compiler deduce all the numbers: template <typename T, typename U, typename V, typename W> requires Numerics<T,U,V,W> auto foo(T arg1, U arg2, V arg3, W arg4) { return 0.0 + arg1 + arg2 + arg3 + arg4; } std::cout << foo (1,2.0,3.0f,4.0l) << "\n"; Compiler deduces all argument types like expected: auto foo<int, double,