开发者

Can an inner class of a template class be a non-template class?

开发者 https://www.devze.com 2023-03-21 18:25 出处:网络
I am making a template class with an inner utility class.All specializations of the template want the same inner class:

I am making a template class with an inner utility class. All specializations of the template want the same inner class:

template<...> class Outer {
    class Inner { };
};

That gives me Outer<...>::Inner but I want all Inner to be the same type, as if I'd just written:

class Inner { };
template <...> class Outer { };

or if Outer were simply not a template class:

class Outer {
    class Inner { };
};

giving me Outer::Inner. I'd like to have Outer::Inner wor开发者_如何学Gok for all Outer<> if that's possible (just for namespace/clarity reasons). Otherwise of course I can just move Inner out.


The nested class can be a non-template, but every instantiation of the template will have its own nested class because they're (otherwise) unrelated types. You can do

namespace detail {

class Inner {};

} // detail

template<...>
class Outer {
    typedef detail::Inner Inner;
};


The way I've done this in the past is using inheritance:

class DummyBase{
protected:
    class Inner{
        //etc...
    };
};

template<...> class Outer : public DummyBase{
    //etc...
};


It will be unique for each instantiation of Outer. I.e.,

Outer<int>::Inner will be a different type from Outer<double>::Inner
0

精彩评论

暂无评论...
验证码 换一张
取 消