'‘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!



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

GCC (libstdc++ in this case) implements several type-trait with different, non-standard names as per an earlier version of the standardization proposal for type traits. Specifically:

std::has_trivial_copy_constructor<int>::value

This only provides part of the information that a full implementation of std::is_trivially_copyable would provide, as having a trivial copy constructor is necessary but not sufficient for a trivially copyable type.

Live example

Solution 4:[4]

Just FYI, these traits are now implemented and will ship in GCC 5.

Solution 5:[5]

Indeed, the C++ 2011 implementation of GCC seems to not support ‘is_trivially_copyable’. See point 20.9.4.3 of the status

You can try to install Clang3.4 and compile with option -std=c++1y

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Smitop
Solution 3
Solution 4 Ville Voutilainen
Solution 5 xiawi