template specialization

R

Rahul

Hi Everyone,

I have the following code,


template<typename T>
void funn(T a)
{
printf("function invoked...\n");
}

template<>
void funn<int>(int aa)
{
printf("second function is invoked...\n");
}

void funn(int a)
{
printf("third function is invoked...\n");
}


int main()
{
funn(2);
}


And i get the following output,

third function is invoked...

But i expected a compilation error "funn(int) redefined", does the
standard say anything about this?

Thanks in advance ! ! !
 
V

v.sobkowski

Hi Everyone,

I have the following code,

template<typename T>
void funn(T a)
{
printf("function invoked...\n");

}

template<>
void funn<int>(int aa)
{
printf("second function is invoked...\n");

}

void funn(int a)
{
printf("third function is invoked...\n");

}

int main()
{
funn(2);

}

And i get the following output,

third function is invoked...

But i expected a compilation error "funn(int) redefined", does the
standard say anything about this?

Thanks in advance ! ! !

Cause you have 2 different functions.
1: void funn(int);
2: void funn<int>(int)

If you wanna call template function use "funn<int>(2)"

greets
 
R

Rahul

Cause you have 2 different functions.
1: void funn(int);
2: void funn<int>(int)

If you wanna call template function use "funn<int>(2)"

greets

Yes that invokes the second function. Does it mean compiler actually
creates two copies of fun(int)... shouldn't that cause a compilation
error as overloading of function with same signatures is not valid...
 
R

Rahul

Yes that invokes the second function. Does it mean compiler actually
creates two copies of fun(int)... shouldn't that cause a compilation
error as overloading of function with same signatures is not valid...

after all, templates are just code replacement at compile time instead
of pre-processor time... so wouldn't the compiler see it as two
definitions for the same prototype?
 
J

Juha Nieminen

Rahul said:
Yes that invokes the second function. Does it mean compiler actually
creates two copies of fun(int)... shouldn't that cause a compilation
error as overloading of function with same signatures is not valid...

They are different functions (they behave differently eg. with respect
to linking), and internally name mangling makes them distinct.
 
R

Rahul

They are different functions (they behave differently eg. with respect
to linking), and internally name mangling makes them distinct.

Does the standard say anything explicitly about such cases? I know
that linker is supposed to remove the duplicate copies of template
functions which are created in multiple translational units...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top