Question Details

No question body available.

Tags

c++ c++20 sfinae c++-concepts requires-clause

Answers (1)

Accepted Answer Available
Accepted Answer
March 30, 2026 Score: 8 Rep: 104,314 Quality: Expert Completeness: 50%

Turns out there's a specialized facility for this: std::addlvaluereferencet. It does nothing if forming a reference to T is illegal.


Alternatively, you could change the return type to something like std::conditionalt &. When the condition is true, it'll resolve to some valid type (std::nullptrt &), but the exact type doesn't matter, since the function will be disabled.

Note that std::conditionalt doesn't work, since both branches are instantiated regardless of the condition.


Also, as I've done here, you could rewrite the requires condition to use std::isvoidv. This automatically handles cv-qualified void too.