0

Since there is no template<typename T, typename T> ... is_same declaration, why the compiler doesn't give an error or at least choose template<typename T, typename U> is_same when is_same<int, int> is used?

  1. Shouldn't number of types used in <> match a template with same number of types?
  2. The template<typename T, typename U> ... is_same doesn't have brackets after is_same token, so why does the compiler still generate ... is_same<int, float> = false with brackets?

CODE

template<typename T, typename U>
constexpr bool is_same = false;

template<typename T>
constexpr bool is_same<T,T> = true;

int main()
{
  std::cout << is_same<int, int>;
  std::cout << is_same<int, float>;
}

COMPILER OUTPUT

template<typename T, typename U>
constexpr const bool is_same = false;

template<>
constexpr const bool is_same<int, int> = true;
template<>
constexpr const bool is_same<int, float> = false;

template<typename T>
constexpr bool is_same<T,T> = true;

int main()
{
  std::cout.operator<<(is_same<int, int>);
  std::cout.operator<<(is_same<int, float>);
  return 0;
}
theateist
  • 13,879
  • 17
  • 69
  • 109
  • *"I would expect the compiler to give an error when `is_same`..."* Why? There is clearly an explicit specialization `is_same` available for it that can be used for `is_same`. I would recommend referring to a [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that explains the basics of what are specializations and where and how they can be used. – Jason May 29 '23 at 03:38
  • I changed the wording. But, that's why I'm asking. I didn't understand that part. Can you explain please? When I use `is_same` the compiler looks for token `is_same` that has brackets after it, right? How does it choose `is_same` without brackets for `is_same`? – theateist May 29 '23 at 03:44
  • Basically, There is a primary template available that has two template parameters and so when you wrote `is_same` the compiler compares the two arguments(which are `int` and `int`) against all the available specializations and then it found a specialization that has both the parameter same and so it chooses that specialization. – Jason May 29 '23 at 03:47
  • But, how does it generate `is_same = false` for `is_same`? The template is `bool is_same` and not `bool is_same<...>` – theateist May 29 '23 at 03:51
  • Because when you write `is_same` the "primary template" is the only viable option. – Jason May 29 '23 at 03:53
  • So, shouldn't it have generated `template<> constexpr const bool is_same = false;` and not `template<> constexpr const bool is_same = false;`? – theateist May 29 '23 at 03:54
  • No because `is_same` is a template-name while `is_same` is the actual type and a type-name. **That means `is_same` and `is_same` are two different classes.** – Jason May 29 '23 at 03:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253870/discussion-between-theateist-and-jason). – theateist May 29 '23 at 04:01
  • Btw you're using [partial specialization](https://en.cppreference.com/w/cpp/language/partial_specialization) and not explicit specialization. I mistyped that in my previous comments. – Jason May 29 '23 at 04:01

0 Answers0