[ namespace @ 20.02.2010. 21:36 ] @
Kada istovremeno postoje genericka i negenericka funkcija (obe s istim prototipom) koja se poziva?
Dobijam da se kod int i3=max<int> (i1, i2) poziva negenericka verzija iako sam ocekivala da ce ovo <int> naterati da se pozove genericka verzija.

Code:
#include <iostream>
using namespace std;

template <typename T> 
T max(T i, T j) 
{
    cout<<"Ovo je genericka f-ja"<<endl;
    return (i>j?i:j);
}

int max (int i, int j)
{
    cout<<"Ovo je negenericka f-ja"<<endl;
    return (i>j?i:j);
}

int main()
{
    int i1=1;
    int i2=2;
    float f1=2.5;
    float f2=3.5;

    int i3=max<int> (i1, i2); //negenericku poziva?
    cout<<"max(i1,i2)="<<i3<<endl;

    float f3=max<float> (f1, f2);
    cout<<"max(f1,f2)="<<f3<<endl;

    int i4=max (i1, i2);
    cout<<"max(i1,i2)="<<i4<<endl;

    return 0;
}


[ karas @ 21.02.2010. 15:33 ] @
Uradila si specijalizaciju template-a, procitaj npr. clanak na http://www.gotw.ca/publications/mill17.htm:
Citat:

Nontemplate functions are first-class citizens. A plain old nontemplate function that matches the parameter types as well as any function template will be selected over an otherwise-just-as-good function template.

[ namespace @ 23.02.2010. 07:37 ] @
Ovo je jasno:
Nontemplate functions are first-class citizens. A plain old nontemplate function that matches the parameter types as well as any function template will be selected over an otherwise-just-as-good function template.


Zbunile su me ove recenice od Lasla Krausa:
Dozvoljeno je da istovremeno postoje obicna (negenericka) funkcija s datim prototipom i genericka f-ja s istim prototipom. U tom slucaju, ako se funkcija poziva bez argumenata sablona, pozvace se negenericka funkcija.
Da bi se generisala i pozvala funkcija na osnovu sablona, potrebno je navesti i argumente sablona
i onda je dat primer:
char * max(char *a, char *b); //deklaracija negenericke f-je
char * max(char *a,char *b) {return strcmp(a,b) ? a : b;} // def.negenericke f-je
char * k = max ( “alfa“, “beta“); // poziva se negenericka verzija (uporedjuje tekstove)
char * l = max <char *> ( “alfa“, “beta“); // poziva se genericka verzija (uporedjuje pokazivace)


[ karas @ 24.02.2010. 12:58 ] @
Obrati paznju da
Code:

char* k = max("alfa", "beta");

poziva genericku verziju ako je negenericka definisana sa
Code:

char* max(char* a,char* b)
{
    return strcmp(a, b) ? a : b;
}

jer su "alfa" i "beta" tipa const char*.
Ostalo je, rekao bih, razjasnjeno.