2

MSVC 2010 を使用すると、次の動作が得られます。

template <class T> class Boogy
{
public:
    void Fn( T in )
    {
    }

    void Fn2( const T& in)
    {
    }
};

template <> void Boogy<int>::Fn( int in ) //builds ok
{
}

template <> void Boogy<int*>::Fn( int* in ) //builds ok
{
}

template <> void Boogy<int>::Fn2( const int& in ) //builds ok
{
}

template <> void Boogy<int*>::Fn2( const int*& in ) //DOES NOT BUILD
{
}

typedef int* intStar;
template <> void Boogy<intStar>::Fn2( const intStar& in ) //builds ok
{
}

明らかに、問題を解決するために「ハック」を思いついたのですが、なぜハックが必要なのですか? そして、これを行う必要がありますか?私がいるコードベースには、テンプレートクラスがクラス全体ではなく、メンバー関数の一部を特殊化したインスタンスが多数あります。同僚は、これは許可されていないと断言しています。

ティア。

4

1 に答える 1

5

である必要がありますint * const &。あなたは を持ってT = int *const T = T const = int * constます。

それは「定数への参照」を意味し、「への定数参照」をU const &意味しないことを忘れないでください— C++ の参照変数は常に定数であるため、後者は意味がありません。つまり、再配置することはできません。あなたの場合、これはintへのポインターであり、const-intへのポインターではなく、2つの異なるタイプです。UUU

もちろん、別の特殊化を追加することもできますint const *:

template <> void Boogy<int const *>::Fn2(int const * const & in) { /* ... */ }
于 2012-05-29T08:03:34.887 に答える