1

I'm trying to set up a rather complicated static_assertion to give people a clean error when they call my function. While I seem to be able to make it work, I don't understand why. I managed to boil it down to a relatively simple case that I don't understand (std::is_same is just used because it's simple). The following compiles happily:

template<typename T> constexpr bool IsInt(const T& t){
  return std::is_same<decltype(t), const int&>::value;
}
template<typename T> bool CheckIsInt(const T& t){
  auto s = t;
  static_assert(IsInt(s), "T should be int");
  return true;
}

On the other hand, if I get rid of the auto s = t, and just use IsInt(t), it fails:

template<typename T> bool CheckIsInt(const T& t){
  static_assert(IsInt(t), "T should be int");
  return true;
}

with the message (clang++-7):

static_assert expression is not an integral constant expression

What is going on? What's the difference?

Evg
  • 25,259
  • 5
  • 41
  • 83
Zolom
  • 11
  • 1
  • The problem is that `t` is a reference. I'll find a dupe question with an explanation. – Evg Jul 18 '20 at 08:45

0 Answers0