Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to express concept requirements for data members in a concept?

what's the best way to define a concept with a nested concept requirements for a data member of the type? Something along these lines:

template<typename T>
concept MyConcept = requires(T a) {    
    {a.something} -> std::integral;
};

This doesn't work because a.something is picked up as a reference (delctype((a.something))). The best I came up with that works is something like this that forces an rvalue:

constexpr auto copy = [](auto value) { return value; };

template<typename T>
concept MyConcept = requires(T a) {    
    {copy(a.something)} -> std::integral;
};

Do I have any better options?

like image 586
dcmm88 Avatar asked Oct 12 '25 11:10

dcmm88


1 Answers

The downside of copy is that it can create false positives for you. A reference member will decay to a value. The only way to ensure the actual type of the member is analyzed, is to write an explicit nested requirement.

template<typename T>
concept MyConcept = requires(T a) {    
    requires std::integral<decltype(a.something)>;
};
like image 185
StoryTeller - Unslander Monica Avatar answered Oct 13 '25 23:10

StoryTeller - Unslander Monica