Opened 11 years ago

Closed 10 years ago

#6655 closed Bugs (fixed)

Inclusion of <boost/signals2.hpp> causes valid code to fail to compile with Clang

Reported by: Alexander Korsunsky <fat.lobyte9@…> Owned by: Frank Mori Hess
Milestone: Boost 1.50.0 Component: signals2
Version: Boost 1.49.0 Severity: Problem
Keywords: Cc:

Description

The following well-formed program does not compile with Clang 3.1 (from trunk):

#include <memory>
#include <boost/signals2.hpp>

int main()
{
    std::shared_ptr<int> s;
}

The error message is:

signals2-bug.cpp:6:26: error: implicit instantiation of undefined template
      'std::shared_ptr<int>'
    std::shared_ptr<int> s;
                         ^
/home/alexander/usr/local/include/boost/signals2/detail/foreign_ptr.hpp:24:30: note: 
      template is declared here
  template<typename T> class shared_ptr;
                             ^

The header boost/signals2/detail/foreign_ptr.hpp declares two names in the std:: namespace:

#if !defined(BOOST_INTEL_STDCXX0X)
namespace std
{
  template<typename T> class shared_ptr;
  template<typename T> class weak_ptr;
}
#endif

which is undefined behaviour, according to the Standard 17.6.4.2.1:

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

The compilation succeeds, if either the #include of <boost/signals2.hpp> is removed, or the offending lines are commented out.

Please remove the offending lines from the header.

Change History (3)

in reply to:  description comment:1 by Michel Morin <mimomorin@…>, 11 years ago

The header boost/signals2/detail/foreign_ptr.hpp declares two names in the std:: namespace which is undefined behaviour, according to the Standard 17.6.4.2.1

Yep. It's not standard-conforming to forward-declare the standard library types (though some standard libraries allow it). libc++ uses an inline namespace, and so forward declarations in namespace std causes compile errors.


Please remove the offending lines from the header.

The solution is to remove the forward declarations and to include <memory>.

comment:2 by Frank Mori Hess, 11 years ago

(In [77289]) Try to deal with C++11 (or not) compilers a little better, so we can avoid standard violation in foreign_ptr.hpp. Refs #6655.

comment:3 by Frank Mori Hess, 10 years ago

Resolution: fixed
Status: newclosed

(In [79273]) Merge from trunk. Fixes #6655

Note: See TracTickets for help on using tickets.