最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Result of expression convertible to a type that satisfies a concept - Stack Overflow

programmeradmin2浏览0评论

In a requirement expression (pardon if my terminology is incorrect) in each requirement there is an expression and optionally a type-constraint. The latter has to be a concept. For example:

template<typename T>
concept HasBar = requires( T t ) { { t.bar } -> std::convertible_to<float>; };

Similarly to std::convertible_to, one can make up a concept called decays_to or something.

What is then the most simple and clean way to require that an expression results in something that is convertible-to / decays-to a concept (well, some type that satisfies a concept) like std::floating_point, std::integral, MyConcept, etc? Is one supposed to make semi-duplicates of these concepts? Like: ConvertibleToFloatingPoint, DecaysToFloatingPoint, DecaysToMyConcept, etc?

In a requirement expression (pardon if my terminology is incorrect) in each requirement there is an expression and optionally a type-constraint. The latter has to be a concept. For example:

template<typename T>
concept HasBar = requires( T t ) { { t.bar } -> std::convertible_to<float>; };

Similarly to std::convertible_to, one can make up a concept called decays_to or something.

What is then the most simple and clean way to require that an expression results in something that is convertible-to / decays-to a concept (well, some type that satisfies a concept) like std::floating_point, std::integral, MyConcept, etc? Is one supposed to make semi-duplicates of these concepts? Like: ConvertibleToFloatingPoint, DecaysToFloatingPoint, DecaysToMyConcept, etc?

Share Improve this question asked Jan 31 at 17:02 NewlineNewline 9735 silver badges13 bronze badges 2
  • "convertible to a concept" would require to check all types. For ConvertibleToFloatingPoint, would you consider a type which convert to double but not float to validate your concept? – Jarod42 Commented Jan 31 at 17:12
  • @Jarod42 On a first thought I probably would consider, but on second thought probably not, so I see what you are saying. – Newline Commented Jan 31 at 17:38
Add a comment  | 

1 Answer 1

Reset to default 3

"Convertible to some T matching concept C" is fundamentally impossible, the compiler isn't going to check all possible conversion targets.

"Decays to a type matching concept C" is possible, but not with the prettiest syntax.

It can't be DecaysTo<std::convertible_to<float>> because concepts can't be passed as template parameters. Traits can be passed though:

template <typename T, template <typename...> typename Trait, typename ...P>
concept DecaysTo = Trait<std::decay_t<T>, P...>::value;

And then: { t.bar } -> DecaysTo<std::is_convertible, float>;

Or, a less convoluted approach would be:

template<typename T>
concept HasBar = requires(T t)
{
    requires std::convertible_to<std::decay_t<decltype(t.bar)>, float>;
};

Or you can cheat by forcing the decay in the expression, e.g.:

template<typename T>
concept HasBar = requires(T t)
{
    { auto(t.bar) } -> std::convertible_to<float>;
};

But this requires bar to be copyable, so IMO this is worse.

发布评论

评论列表(0)

  1. 暂无评论