This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/54722] New: std::is_nothrow_default_constructible<T>::value depends on whether destructor throws or not.


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54722

             Bug #: 54722
           Summary: std::is_nothrow_default_constructible<T>::value
                    depends on whether destructor throws or not.
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: cassio.neri@gmail.com


Consider:

#include <iostream>
#include <type_traits>

struct foo {
  foo() noexcept {}
  ~foo() {}
};

int main() {
  std::cout << std::boolalpha;
  std::cout << std::is_nothrow_default_constructible<foo>::value << std::endl;
  return 0;
}

This should output 'true' but it outputs 'false'. Adding a 'noexcept'
specification to ~foo() makes the code to output the expected result.

Looking at the source, I guess, the reason lies on the implementation of this
helper class:

template<typename _Tp>
  struct __is_nt_default_constructible_atom
  : public integral_constant<bool, noexcept(_Tp())>
  { };

Indeed, the expression '_Tp()' if executed creates a temporary of type _Tp
whose lifetime ends immediately and ~Tp_ is called. Therefore,
'noexcept(_Tp())' is 'true' if and only if neither the constructor nor the
destructor throw.

I believe the below implementation fixes the problem (at least it does for the
example above):

template<typename _Tp>
  struct __is_nt_default_constructible_atom
  : public std::integral_constant<bool, noexcept(new (std::nothrow) _Tp)>
  { };


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]