Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template doubt in C++

Tags:

c++

templates

#include <iostream>
using namespace std;
template<typename T> void test()
{
     cout << "Called from template T";
}
template<int I> void test()
{
     cout << "Called from int";
}
int main()
{
     test<int()>();      
}

In the above snippet test<int()>() calls the first version and gives output

Called from template T

Why doesn't the second version get called?

like image 830
Rahul Garg Avatar asked Sep 25 '10 05:09

Rahul Garg


Video Answer


1 Answers

As per ISO C++03 (Section 14.3/2)

In a template-argument, an ambiguity between a type-id and an expression is resolved to a type-id. int() is a type-id so the first version gets called.

like image 77
Prasoon Saurav Avatar answered Oct 27 '22 18:10

Prasoon Saurav