boost::ref issue

Hi All,

I’m trying to compile a C++ project with Clang. It compiles with gcc without error, but Clang breaks. Unfortunately could not simplify further and has ‘boost’ as dependency. I need some help.

How can I change the code to compile with clang? And if it is a not expected behavior of Clang, what could be an error report for this?

Thanks,
Laszlo

$ clang++ -c test.cpp
test.cpp:9:64: error: no matching function for call to ‘ref’
std::for_each(v.begin(), v.end(), boost::bind(&S::bar, boost::ref(this), _1));
^~~~~~~~~~
/usr/include/boost/ref.hpp:64:63: note: candidate function [with T = S *] not viable: no known conversion from ‘S *’ to ‘S *&’ for 1st argument
template inline reference_wrapper BOOST_REF_CONST ref(T & t)
^
1 error generated.

test.cpp (267 Bytes)

I reduced your testcase:

   template <typename T> void ref(T &) { return 8; }
   struct S { int foo() { return ref(this); } };

I think it is GCC being wrong there. You should not be able to take a
reference from 'this'.

You can try boost::cref, or something like
  S *that = this; ... boost::ref(that)

Hi All,

I’m trying to compile a C++ project with Clang. It compiles with gcc
without error, but Clang breaks. Unfortunately could not simplify further
and has ‘boost’ as dependency. I need some help.

How can I change the code to compile with clang? And if it is a not
expected behavior of Clang, what could be an error report for this?

Thanks,
Laszlo

I reduced your testcase:

template void ref(T &) { return 8; }
struct S { int foo() { return ref(this); } };

I think it is GCC being wrong there. You should not be able to take a
reference from ‘this’.

You can try boost::cref, or something like
S *that = this; … boost::ref(that)

It’s always arguable what the “type” of this is, seeing as it’s an artifact, but seeing as you cannot reasonable mutate this itself (the pointer, not the pointee), I always think about it as being T* const.

– Matthieu