0

I am compiling below with g++ 11.2.0,

#include <iostream>

using namespace std;

template <typename T>
void fun(T) { cout << 1 << endl; }
template <>
void fun(int*) { cout << 2 << endl; }
template <typename T>
void fun(T*) { cout << 3 << endl; }
int main()
{
    int* x{};
    double* y{};
    fun(1);
    fun(x);
    fun(y);
    return 0;
}

It gives the below output,

1
3
3

I was expecting 1, 2, 2 as output as fun(x) should call a specialized template function.

Can you explain this behaviour?

Jagdish
  • 1,848
  • 3
  • 22
  • 33

0 Answers0