has_trivial_destructor improvable?
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Feb 23 11:17:00 GMT 2012
N.B. I was forgetting the has_trivial_xxx traits were renamed/replaced
by http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3142.html
but GCC doesn't implement that yet.
I'm not sure, but I think std::is_trivially_default_constructible
should be true for your optional<std::string> union, because although
the default constructor is not trivial (in terms of the core language)
it doesn't actually call std::string's constructor or any other
non-trivial functions.
On 22 February 2012 18:24, Hite, Christopher wrote:
>
> The whole point of N2544 was to allow you to do stuff like this. I can declare a deconstructor/constructor for the union which does nothing. Unfortunately has_trivial_destructor doesn't get it.
Because it's not trivial. The is_trivial_xxx traits tested very
specific properties with well-defined meanings according to the
standard, they didn't just test for empty constructors/destructors,
because that's not the same thing.
struct X {
std::string s;
};
Even though it's not user-declared, X does not have a trivial default
constructor, because it calls std::string's default constructor which
is non-trivial.
But as N3142 says, that's not very useful for library writers.
> That's just my use case though. A more general question: can you detect do nothing/trivial constructors/destructors/assignment on any struct/union/class?
Again, do-nothing and trivial are not the same thing. And a do-nothing
constructor for a struct/class (which default-initiailizes bases and
members) is not the same as a do-nothing constructor for a union
(which does not initialize its members.)
The unimplemented is_trivially_xxx traits should work for both cases
if the constructor really is do-nothing, unlike the X example above,
or this example which allocates memory in its default constructor:
struct Y {
std::string s = "foo";
Y() { }
};
> For example if std::pair<bool,char> defines a copy constructor or assignment does it have to lose the "trivial" property?
It would, yes, but that's why std::pair has defaulted copy/move
constructors, so those members are trivial if the template arguments
are trivially constructible.
> Is there some proof that doing these things are impossible?
Not that I know of, but I'd be surprised if is_trivially_xxx or the
old has_trivial_xxx can be implemented without compiler magic.
More information about the Gcc-help
mailing list