std::is_trivial_default_constructible<T>, what's required?!

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

This doesn't even compile (leaving x_ uninitialized compiles but I don't see why it's not trivial to initialize a POD member inline):

class Test {
 public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement 'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test...?!

Thanks!
Gab

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev
> A default constructor is trivial if it is not user-provided and if ... no non-static data member of its class has a default member initializer ...


2018-01-25 15:11 GMT+03:00 Gabriel Charette via cfe-dev <[hidden email]>:
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

This doesn't even compile (leaving x_ uninitialized compiles but I don't see why it's not trivial to initialize a POD member inline):

class Test {
 public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement 'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test...?!

Thanks!
Gab

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev
Ah ah! Had missed that. That's unfortunate :(

Thanks

On Thu, Jan 25, 2018 at 1:44 PM Furkan Usta <[hidden email]> wrote:
> A default constructor is trivial if it is not user-provided and if ... no non-static data member of its class has a default member initializer ...


2018-01-25 15:11 GMT+03:00 Gabriel Charette via cfe-dev <[hidden email]>:
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

This doesn't even compile (leaving x_ uninitialized compiles but I don't see why it's not trivial to initialize a POD member inline):

class Test {
 public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement 'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test...?!

Thanks!
Gab

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev
In reply to this post by Artem Dergachev via cfe-dev

On Thu, Jan 25, 2018 at 4:11 AM, Gabriel Charette via cfe-dev <[hidden email]> wrote:
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

To be trivially default constructible means to be default constructible *without doing anything*.
 

This doesn't even compile (leaving x_ uninitialized compiles but I don't see why it's not trivial to initialize a POD member inline):

class Test {
 public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement 'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test...?!


If you need to initialize something, that's not trivial.

-- James 

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev


On Thu, Jan 25, 2018 at 4:32 PM James Dennett <[hidden email]> wrote:
On Thu, Jan 25, 2018 at 4:11 AM, Gabriel Charette via cfe-dev <[hidden email]> wrote:
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

To be trivially default constructible means to be default constructible *without doing anything*.

Right... What I want is way to tell if the compiler will evaluate this at compile-time (e.g. to guarantee no static initializer for globals of trivial types with only zero-initialized POD members).
 
 

This doesn't even compile (leaving x_ uninitialized compiles but I don't see why it's not trivial to initialize a POD member inline):

class Test {
 public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement 'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test...?!


If you need to initialize something, that's not trivial.

-- James 

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: std::is_trivial_default_constructible<T>, what's required?!

Artem Dergachev via cfe-dev
On Thu, Jan 25, 2018 at 7:35 AM, Gabriel Charette <[hidden email]> wrote:


On Thu, Jan 25, 2018 at 4:32 PM James Dennett <[hidden email]> wrote:
On Thu, Jan 25, 2018 at 4:11 AM, Gabriel Charette via cfe-dev <[hidden email]> wrote:
Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

To be trivially default constructible means to be default constructible *without doing anything*.

Right... What I want is way to tell if the compiler will evaluate this at compile-time (e.g. to guarantee no static initializer for globals of trivial types with only zero-initialized POD members).

`constexpr` (or the [[clang::require_constant_initialization]] attribute) is what you want to ensure compile-time initialization.  Happily it can do a lot more than zero-initialization.

-- James

p.s. This isn't really the right list for general questions about C++.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev