Reputation: 4284
A union in C++98 cannot contain a class that has virtual functions or nontrivial constructors or destructors.
what are the situations you know where the use of POD is must?
Upvotes: 1
Views: 474
Reputation: 145309
One case where PODness is required is for direct conversion to/from sequence of bytes.
Another case where PODness is required is where the type must guarantee that instantiation has no effect whatsoever other than using a bit of memory, e.g. as in the following class:
namespace detail {
using std::vector;
template< class Type, Podness::Enum podNess = Podness::isPod >
class ValueWrapper_
{
private:
Type v_;
public:
Type const& ref() const { return v_; }
ValueWrapper_() {} // No initialization
ValueWrapper_( Type const v ): v_( v ) {}
};
template< class Type >
struct ValueWrapper_< Type, Podness::isNotPod >
{
private:
vector<Type> v_; // General but incurs allocation overhead.
public:
Type const& ref() const { return v_[0]; }
ValueWrapper_() {} // Supports apparent no initialization.
ValueWrapper_( Type const v ): v_( 1, v ) {}
};
} // namespace detail
template< class Type >
class Optional_
{
private:
typedef detail::ValueWrapper_<Type, Podness_<Type>::value > Wrapper;
Wrapper const value_;
bool const isNone_;
Optional_& operator=( Optional_ const& ); // No such.
public:
bool isNone() const { return isNone_; }
Type const& value() const
{
hopefully( !isNone_ )
|| throwX( "Optional_::value(): there is no value" );
return value_.ref();
}
Optional_(): isNone_( true ) {}
Optional_( Type const& v ): value_( v ), isNone_( false ) {}
static Optional_ none() { return Optional_(); }
};
template<>
class Optional_< void >
{
private:
Optional_& operator=( Optional_ const& ); // No such.
public:
bool isNone() const { return true; }
void value() const
{
throwX( "Optional_::value(): there is no value" );
}
Optional_() {}
static Optional_ none() { return Optional_(); }
};
Hm, I can't think of any third case…
Upvotes: 2
Reputation: 3306
When interacting with a non C++ API (typically a C style API).
Upvotes: 2
Reputation: 263202
The syntax
X a = {1, 2, 3};
only works with PODs, although this restriction is lifted with std::initializer_list
in C++11.
Upvotes: 2