80

In boost/mpl/assert.hpp, I saw something like this:

template<class Pred>
struct eval_assert {
    typedef typename extract_assert_pred<Pred>::type P;
    typedef typename P::type p_type;
    typedef typename ::boost::mpl::if_c<p_type::value,
        AUX778076_ASSERT_ARG(assert<false>),
        failed ************ P::************
    >::type type;
};

If the first ************ can be treated as pointers of struct failed, the P::************ really doesn't make any sense to me. Is this standard C++?

stoneyan
  • 871
  • 6
  • 8
  • 1
    Yes. It's a multiple-level pointer to pointer to member of `P`. – T.C. Nov 19 '14 at 20:06
  • 1
    ( Such nested pointers exist? :/ ) – deviantfan Nov 19 '14 at 20:07
  • 38
    Pointerception ... – Jakub Arnold Nov 19 '14 at 20:08
  • 5
    @deviantfan In production code? You would be surprised. ;) But the point here is to cause a compilation failure by trying to refer to a member of `P` with a very high degree of certainty that it won't exist. (In C++11 you'd probably just use `static_assert(false)` instead, but of course Boost has to be portable to pre-C++11.) – cdhowie Nov 19 '14 at 20:09
  • 4
    Side note: The 12-level pointer probably has something to do with the minimum required by the C standard. – T.C. Nov 19 '14 at 20:52
  • 29
    @PaulDraper it's actually reading [_`hunter2hunter2hunter2hunter2`_](http://www.bash.org/?244321) here – sehe Nov 20 '14 at 00:01
  • 5
    pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a member of type P – Kai Nov 20 '14 at 15:45
  • I think I've seen this used as a way to make hacking a game more difficult. – Lauri Elias Nov 26 '14 at 07:48

3 Answers3

100

The point of this code is to help the compiler produce "visible" error messages.

In pre static_assert era, compiling a template-heavy code could easily produce ~100 lines of error messages even for a single mistake, and 99% of those lines are often meaningless.

The 10 pointers trick is useful to point out the actual error, for example:

 BOOST_STATIC_ASSERT((std::is_same<T,U>));

With T=void* and U=char* compiled with gcc produces ~10 error lines, but you can easily see the relevant one:

error: no matching function for call to ‘assertion_failed(mpl_::failed************ std::is_same<void*, char*>::************)’
sbabbi
  • 11,070
  • 2
  • 29
  • 57
45

It's a pointer-to-pointer-to-...-member of type P, where the member is a data member of type pointer-to-pointer-to-...-failed.

In this case the goal is simply to cause compilation to fail by referring to a member of P with a very high degree of probability that it won't exist. In C++11 you'd just use static_assert instead, but of course Boost needs to be portable to pre-C++11 dialects.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
19

F P::* is a "pointer to member of P of type F".

F P::** is a "pointer to pointer to member of P of type F".

More *s adds more "pointer to" in front.

In this case, F is failed ************, i.e., "pointer to pointer to ... pointer to failed".

T.C.
  • 133,968
  • 17
  • 288
  • 421