Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘is_trivially_copyable’ is not a member of ‘std’

My gcc version is 4.8.3 20140624. I can use is_pod, is_trivial, is_standard_layout, but fail when trying is_trivially_copyable, is_constructible and is_default_constructible, maybe more. The error message is 'xxx' is not a member of 'std'.

What's the problem here? Are they even supported by the current GCC? Thanks!

like image 855
user3156285 Avatar asked Aug 04 '14 16:08

user3156285


People also ask

Is STD string trivially copyable?

The diagnostic is not required, but std::atomic requires a trivially copyable type and std::string is not one.

Is trivially copy constructible?

A trivially copy constructible type is a type which can be trivially constructed from a value or reference of the same type. This includes scalar types, trivially copy constructible classes and arrays of such types.


2 Answers

As others mention, GCC versions < 5 do not support std::is_trivially_copyable from the C++11 standard.

Here is a hack to somewhat work around this limitation:

// workaround missing "is_trivially_copyable" in g++ < 5.0
#if __GNUG__ && __GNUC__ < 5
#define IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
#else
#define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
#endif

For common cases, this hack might be enough to get your code working. Beware, however, subtle differences between GCC's __has_trivial_copy and std::is_trivially_copyable. Suggestions for improvement welcome.

like image 162
Kipton Barros Avatar answered Sep 22 '22 12:09

Kipton Barros


Some of them are not implemented. If we look at libstdc++'s c++11 status page:

Type properties are listed as partially implemented.

They list as missing:

  • is_trivially_copyable
  • is_trivially_constructible
  • is_trivially_default_constructible,
  • is_trivially_copy_constructible
  • is_trivially_move_constructible
  • is_trivially_assignable
  • is_trivially_default_assignable
  • is_trivially_copy_assignable
  • is_trivially_move_assignable

That being said:

is_constructible and is_default_constructible should be available. I can use them successfully in GCC 4.8.2.

#include <type_traits>
#include <iostream>

int main() {
    std::cout << std::is_constructible<int>::value << "\n";
    std::cout << std::is_default_constructible<int>::value << "\n";
}

 

[11:47am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/g++ -std=c++11 foo.cc
[11:47am][wlynch@apple /tmp] ./a.out 
1
1
like image 41
Bill Lynch Avatar answered Sep 18 '22 12:09

Bill Lynch