首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::mpl::or_和boost::mpl::and_的不同行为?

boost::mpl::or_和boost::mpl::and_的不同行为?
EN

Stack Overflow用户
提问于 2012-09-14 14:14:32
回答 2查看 715关注 0票数 0

下面的代码试图测试boost::mpl::or_boost::mpl::and_的短路行为。

代码语言:javascript
复制
#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;
}

虽然我预计由于类型定义不完整,这段代码在编译时会失败,但它实际上是有效的:

代码语言:javascript
复制
>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

所以,我想了解的是:

boost::mpl::or_ boost::mpl::and_ 计算它们的参数的方式是否不一致?或者,最有可能的是,我无法捕捉到的代码中有错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-14 14:22:16

为什么您认为or_的第二个参数不会被计算?is_scalar可以与未定义的类型一起使用。

例如,在这种情况下,您也会得到您的错误。

代码语言:javascript
复制
// 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>
>
{};

因为boost::is_scalar<T>是真,但在您的情况下boost::is_scalar<dummy<T>>是假的,所以。

看。http://liveworkspace.org/code/a792e18ca16a0410a67a6eee8c550bd9

票数 2
EN

Stack Overflow用户

发布于 2012-09-14 14:35:57

您的想法有一个逻辑错误- ||将短路iif第一个参数评估为trueis_scalar<std::vector<double>>false,这意味着需要计算第二个参数。相反,&&将短路iif,第一个论点是false,这就是上面所说的情况。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12426342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档