Sep 20, 2025

[C++] brace init. disallows narrow conversion, use it for building up can_narrow concept.

1) brace init. disallows narrow conversion 
2) `requires` can only have expression, declaration is not allowed.
(i.e. T t; is not allowed, but T{} ok)
template<typename S, typename T>
concept can_narrow = requires() {
  T {std::declval<S>()};
};

int main() {
  if constexpr (can_narrow<float, int>) {
    cout << "double to int ok";
  } else{
    cout << "double to int not ok"; // hit here.
  }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.