开发者

typedef variable inside same class. Is it a good practice?

开发者 https://www.devze.com 2023-02-01 07:09 出处:网络
Initially I was trying to typedef a template class and I got to the \"gotw 79\" article. And I didn\'t want to create another class so I ended up doing the following. Basically typedef\'ing inside the

Initially I was trying to typedef a template class and I got to the "gotw 79" article. And I didn't want to create another class so I ended up doing the following. Basically typedef'ing inside the same class. It works obviously. but is it a good practice?

template <typename T,typename L>
class MyClass{
     type开发者_运维知识库def std::tr1::shared_ptr<MyClass<T,L> > shrdPtr;
}

Thank you.


Well, I'm not a big fan of it unless you are designing MyClass to be specifically used only within shared_ptr objects, at which point I would insist that requirement be enforced.

It's a little ridiculous to put typedefs for every unrelated template instantiation that you might use with a given object. Just because you might put MyClass in a shared_ptr is not a good reason to typedef it there. You going to put typedefs for std::vector, map, list, unordered_map, set, deque,....etc, etc, etc?

But if MyClass extends shared_from_this and has private/protected constructors so that it can ONLY be created and immediately assigned to a shared_ptr then...sure...it's part of the interface.

If you're trying to avoid having to type out long parameter lists to instantiate a shared_ptr for a templated type with lots of parameters then a better bet is an EXTERNAL utility object just like shown in the article you cited:

template < typename T >
struct instantiate_shared_ptr { typedef shared_ptr<T> type; };

template < typename after typename > struct my_complex_template {};
typedef my_complex_template<some parameters> mct_1;
typedef instantiate_shared_ptr<mct_1>::type mct_1_sp;


Yes, especially if the name MyClass_sp is referred to in client code.


This is probably good practice, it makes it simpler if you decide to change the underlying class the typedef refers to at a later date and (arguably) saves typos as well as making code easier to read even if it never changes. The particular choice of name here MyClass_sp leaves a little to be desired in my opinion though.

Also it's worth thinking carefully if making the typedef public or private is most appropriate, i.e. is it part of your public interface?


It's good, IMO.
I used it a lot.
If I want to use a container which element is the type of the my template, I typedef it.
Such as,

template <typename T>  
class MyClass {  
private:  
    typedef std::list<T> SomeContainerType;  
    typedef SomeContainerType::iterator IteratorType;  

Then if I find any other structure more suitable, such as a vector, I can change the type without touching too much code.


A better solution for smart pointer typedefs is to do them after the class definition in the header:

namespace N
{
    class A
    {
    };

    typedef std::tr1::shared_ptr<A> APtr;
}

This keeps your smart pointer defintion near your class definition while preventing you (and any developers using your code) from having to write code like A::APtr a(new A) (which just looks odd).

EDIT: Since he is concerned with a template class:

namespace N
{
    template<class T, class L>
    class A
    {
    };

    template<class T, class L>
    struct A_Instantiator
    {
        typedef std::tr1::shared_ptr<A<T, L> > APtr;
    };
}
0

精彩评论

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