wieso werden klassen templates genauer als funktions templates ausgewertet?



  • ich weis nicht, ob das gcc spezifisch ist, aber bei klassen wird bei mir immer die am besten passende version rausgesucht, bei funktionen wird das erstbeste genommen, wenn es zufälligerweise eine funktion gibt, bei ders ähnlich rauskommt, wird sofort "uneindeutig" gesagt. ist das normal? was sagt der standard dazu?

    hier ein beispiel:

    template<class T>
    class Foo{};
    
    //spezialisierung für constante typen
    template<class T>
    class Foo<const T>{};
    
    Foo<int*> a;//gibt die erste version
    
    Foo<const int*> b;//gibt die zweite version
    
    //nun die funktionen
    template<class T> void bar(T*){}
    template<class T> void bar(const T*){}
    
    int* i;
    const int* i2; 
    
    bar(i);//ok,erste version
    bar(i2);//fehler,uneindeutig
    


  • Bei mir bringts keinen Fehler...



  • //edit2 hmm hier mal das original...wahrscheinlich is es dochn denkfehler bei mir...

    template<class T,class U>
    std::basic_string<T> convert(U* str,const std::locale& loc=std::locale()){}
    
    template<class T,class U>
    std::basic_string<T> convert(const U* str,const std::locale& loc=std::locale()){}
    

    aufruf:

    convert<char>(L"Hallo\n")
    
    test.cpp:88: error: call of overloaded `convert(const wchar_t[7])' is ambiguous
    
    test.cpp:75: note: candidates are: std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> > 
                       convert(U*, const std::locale&) [with T = char, U = const wchar_t]
    
    test.cpp:79: note: std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> > 
                      convert(const U*, const std::locale&) [with T = char, U = wchar_t]
    

Anmelden zum Antworten