mardi 4 août 2015

C++ template functions strip cv qualifier [duplicate]

Why does the following compile (in MSVC2015 and in clang)? Note that the function was declared with a different signature than the one used for the definition. Adding to the confusion, if the comments are removed (that is now we have void foo(T) and void foo(const T), the compilers complain as if the signatures were the same. Why would template functions strip the const? (asking for the actual rule and for the reason behind it)

#include <iostream>

template< class T >
struct s
{
    void foo( T ); // non-const T
    //void foo( const T ); // const T
};

//template< class T >
//void s< T >::foo( T )
//{
//  ::std::wcout << L"NON-const called\n";
//}

template< class T >
void s< T >::foo( const T )
{
    ::std::wcout << L"const called\n";
}

int main()
{
    s<int> a_S;
    a_S.foo( 7 );

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire