Equality of int&'s in template parameters

Viewed 152

Suppose we have the following program:

template <class T, T n1, T n2>
struct Probe { 
    static const int which = 1;
};

template <class T, T n>
struct Probe<T, n, n> { 
    static const int which = 2;
};


int i = 123;

const int myQuestion = Probe<int&, i, i>::which;

I am pretty sure, that myQuestion should be 2 regardless of the version of C++ standard, but compilers disagree upon it. MSVC and clang say that it is 2 until C++14, and 1 since C++17. See the demo. What is the truth?

My investigation so far:

  • I have found one relevant sentence in the C++ standard. It was there in C++11, C++14, C++17 and C++20. It did not change.
  • If you remove the parameter T from the example code, all compilers agree, that myQuestion is 2. Demo.
0 Answers
Related