Effective C++ Item 42
2009-08-27


nested dependent typename がある時に typename を使う。文脈によっては、変数の定義なのか関数の呼び出しなのかが区別がつかない時がある。それを区別する為に、typename を用いる。

以下の例では、x が C::const_iterator を指すポインタなのか、C::const_iterator と x の掛け算なのかの明確な区別は出来ない。


template<typename C>
void porint2nd(const C& container)
{
    C::const_iterator * x;
}
このでは例は、

template<typename C>
void porint2nd(const C& container)
{
    typename C::const_iterator * x;
}
とする。

他にも基底クラスの型を指定するときにも用いる。


template<typename T>
class Derived: public Base<T>::Nested
{
    public:
        explicit Derived(int x):Base<T>::Nested(x)
        {
            typename Base<T>::Nested temp;
            ...
        }
}

前回次回

[c++(マクロへ移行中)]

コメント(全0件)
コメントをする


記事を書く
powered by ASAHIネット