Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The purpose behind empty struct?

Tags:

c++

pointers

Declarations of auto_ptr from C++ Standard Library

namespace std {

template <class Y> struct auto_ptr_ref {};


template <class X>
class auto_ptr {
public:
    typedef X element_type;

    // 20.4.5.1 construct/copy/destroy:
    explicit           auto_ptr(X* p =0) throw();
                       auto_ptr(auto_ptr&) throw();
    template <class Y> auto_ptr(auto_ptr<Y>&) throw();

    auto_ptr&                      operator=(auto_ptr&) throw();
    template <class Y> auto_ptr&   operator=(auto_ptr<Y>&) throw();
    auto_ptr&                      operator=(auto_ptr_ref<X>) throw();

    ~auto_ptr() throw();

    // 20.4.5.2 members:
    X&     operator*() const throw();
    X*     operator->() const throw();
    X*     get() const throw();
    X*     release() throw();
    void   reset(X* p =0) throw();

    // 20.4.5.3 conversions:
                                auto_ptr(auto_ptr_ref<X>) throw();
    template <class Y> operator auto_ptr_ref<Y>() throw();
    template <class Y> operator auto_ptr<Y>() throw();
};

}

I don't understand the purpose of this part:

template <class Y> struct auto_ptr_ref {};

Without declaring any variable, how can these be valid:

auto_ptr&                      operator=(auto_ptr_ref<X>) throw();

and these too:

auto_ptr(auto_ptr_ref<X>) throw();
    template <class Y> operator auto_ptr_ref<Y>() throw();

Edit: and also (I just notice) I don't understand how the "operator" are used for the last two lines. Isn't the syntax something like "return-type operatoroperand;", where is the return type? operand?

like image 960
user385261 Avatar asked Jul 23 '10 08:07

user385261


People also ask

What is the use of empty struct?

Basically an empty struct can be used every time you are only interested in a property of a data structure rather than its value (this will be more clear in the practical examples).

What does an empty struct mean?

The empty struct is a struct type that has no fields.

What is the use of empty structure in C?

Empty struct in C is undefined behaviour (refer C17 spec, section 6.7. 2.1 ): If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.

Why sizeof empty struct is 1?

This is because that would make it possible for two distinct objects to have the same memory location. This is the reason behind the concept that even an empty class and structure must have a size of at least 1.


1 Answers

Google search for "auto_ptr_ref" reveals this detailed explanation.

I don't quite get that explanation, but looks like it is for the following. Without that trick you could pass an auto_ptr into a function that would get ownership of the object and assign your variable to a null pointer. With the extra class trick above you will get a compile error in such case.

like image 153
sharptooth Avatar answered Sep 28 '22 08:09

sharptooth