Question

The code below tries to test the short circuit behavior of boost::mpl::or_ and boost::mpl::and_:

#include <vector>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/and.hpp>
#include <boost/type_traits/is_scalar.hpp>

// Dummy is forward declared and never defined
template <class T> class dummy;

// If T is a scalar evaluates to T without trying to compute the result of 
// boost::mpl::is_scalar< dummy<T>, otherwise it fails at compile time.
template <class T> 
class testOr 
: public boost::mpl::eval_if< 
    boost::mpl::or_< boost::is_scalar<T>, boost::is_scalar< dummy<T> > >,
    boost::mpl::identity<T>,
    dummy<T>        
>
{};

// If T is not a scalar evaluates to T without trying to compute the result of 
// boost::mpl::is_scalar< dummy<T>, otherwise it should fail at compile time.
template <class T> 
class testAnd
: public boost::mpl::eval_if< 
    // It appears that is_scalar< dummy<T> > is not instantiated and the operation
    // evaluates to false
    boost::mpl::and_< boost::is_scalar<T>, boost::is_scalar< dummy<T> > >,
    dummy<T>,
    boost::mpl::identity<T>
>
{};

int main() {    

  static_assert(boost::is_same< testOr< double >::type, double>::type::value,"Fails at compile time");
  // The following line causes failures at compile time due to incomplete type definition
  //static_assert(boost::is_same< testOr< std::vector<double> >::type, double>::type::value,"Fails at compile time");

  static_assert(boost::is_same< testAnd< std::vector<double> >::type, std::vector<double> >::type::value,"Fails at compile time");
  // The following should cause failure at compile time due to incomplete type definition, but works instead!
  static_assert(boost::is_same< testAnd< double >::type , double >::type::value,"Fails at compile time");

return 0;
}

While I expect this code to fail at compile time due to an incomplete type definition, it actually works:

>icpc --version
icpc (ICC) 12.1.3 20120212
Copyright (C) 1985-2012 Intel Corporation.  All rights reserved.

>icpc -gcc-name=gcc-4.5 -std=c++0x -o ex-4.0.x ex-4.0.cc

So, the point I would like to understand is:

Is there an inconsistency in the way boost::mpl::or_ and boost::mpl::and_ evaluate their arguments or, most likely, is there an error in the code I am not able to catch?

Was it helpful?

Solution

Why you think, that second argument of or_ will not be evaluated? is_scalar can be used with undefined types.

For example in this case, you get your error as well.

// If T is not a scalar evaluates to T without trying to compute the result of 
// boost::mpl::is_scalar< dummy<T>, otherwise it should fail at compile time.
template <class T> 
class testAnd
: public boost::mpl::eval_if< 
    // It appears that is_scalar< dummy<T> > is not instantiated and the operation
    // evaluates to false
    boost::mpl::and_< boost::is_scalar<T>, boost::is_scalar< dummy<T> > >,
    boost::mpl::identity<T>,
    dummy<T>
>
{};

Since boost::is_scalar<T> is true, but boost::is_scalar<dummy<T>> is false in your case, so.

Look. http://liveworkspace.org/code/a792e18ca16a0410a67a6eee8c550bd9

OTHER TIPS

You're having a logic error in your thoughts - || will short circuit iif the first argument evaluates to true. is_scalar<std::vector<double>> is false, meaning the second argument needs to be evaluated. In contrast, && will short circuit iif the first argument is false, which is the case as said above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top