首页 > 解决方案 > 可以使 declval 不为 Visual Studio 2010 返回不正确的类型

问题描述

我有以下代码并尝试在 Visual Studio 2010 中编译它。我必须支持旧客户,因此目前无法升级。

#include <boost/utility.hpp>
#include <boost/static_assert.hpp>
#include <type_traits>

struct Foo {
    static Foo CreateFoo(int p){return Foo();}
};

template <typename F, typename A0>
struct ResultOf {
    typedef decltype(boost::declval<F>()(boost::declval<A0>())) Type;
};

typedef ResultOf<decltype(&Foo::CreateFoo),int>::Type ResultType;

BOOST_STATIC_ASSERT(!std::is_reference<ResultType>::value);

静态断言失败,因为返回的是引用类型而不是值类型。

我知道 Visual Studio 2010 decltype 支持是粗略的。但是是否可以仅通过修改 decltype 表达式来使上述工作正常

typedef decltype(boost::declval<F>()(boost::declval<A0>())) ResultType;

我只需要对接受一个参数并返回值引用的函数提供可靠的支持。

我还注意到,如果 CreateFoo 是一个自由函数而不是 Foo 的静态方法,那么代码就会编译。如果我找不到更好的东西,它们可能是一种解决方法。

标签: c++visual-studio-2010decltype

解决方案


推荐阅读