gpt4 book ai didi

c++ - boost::mpl::or_ 和 boost::mpl::and_ 的不同行为?

转载 作者:行者123 更新时间:2023-11-27 23:21:44 28 4
gpt4 key购买 nike

下面的代码尝试测试 boost::mpl::or_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;
}

虽然我预计此代码会由于类型定义不完整而在编译时失败,但它确实有效:

>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_ 评估它们的参数的方式是否存在不一致,或者最有可能的是,代码中是否存在我无法捕获的错误?

最佳答案

你为什么这么想,or_ 的第二个论点|不会评价? is_scalar可以与未定义的类型一起使用。

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

// 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

关于c++ - boost::mpl::or_ 和 boost::mpl::and_ 的不同行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426342/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com