[cfe-dev] private class members access fails with meta-programming in clang?

John McCall rjmccall at apple.com
Wed Apr 24 23:27:23 PDT 2013


On Apr 24, 2013, at 9:20 AM, Ramneek Handa <ramneekhanda at gmail.com> wrote:
> I thought this should pass with clang.
> 
> x-- start --x
> 
> #include <boost/mpl/has_xxx.hpp>
> 
> BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(hasSomething, Data, false);
> 
> class A {
> private:
> struct Data {
> };
> };
> 
> 
> void function(bool val = hasSomething<A>::value) {}
> 
> 
> x-- end --x
> 
> however it produces following output:
> test_private_data.cpp:3:49: error: 'Data' is a private member of 'A'
> BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(hasSomething, Data, false);
> ^
> /usr/include/boost/mpl/has_xxx.hpp:195:68: note: expanded from macro 'BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF'
> , boost::mpl::aux::type_wrapper<BOOST_MSVC_TYPENAME U::name>* = 0 \
> ^
> test_private_data.cpp:3:1: note: while substituting deduced template arguments into function template 'test' [with U = A]
> BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(hasSomething, Data, false);
> ^
> /usr/include/boost/mpl/has_xxx.hpp:193:41: note: expanded from macro 'BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF'
> static boost::mpl::aux::yes_tag test( \
> ^
> test_private_data.cpp:12:26: note: in instantiation of template class 'hasSomething<A, mpl_::bool_<false> >' requested here
> void function(bool val = hasSomething<A>::value) {}
> ^
> 1 error generated.
> /usr/bin/ld: /tmp/test_private_data-Hatffv.o: No such file: No such file or directory
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
> 
> 
> 
> 
> BUT it passes with GCC.
> 
> Which one is right?

I assume that Boost is using SFINAE here in its check.  Access control
violations were not subject to SFINAE prior to C++11, and Clang is correct
to diagnose it.  You can compile with -std=c++11 to make this work;  the
trait will return 'false'.

John.




More information about the cfe-dev mailing list