boost :: enable_if MSVC

时间:2012-08-26 19:54:51

标签: c++ boost visual-studio-2012 boost-mpl enable-if

我有代码,在gcc中按预期编译和运行,并且不在MSVC 2012 RC中编译,我无法解释原因,所以它是MSVC中的错误,或者我的错误代码不正确?

#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/size.hpp>
#include <boost/utility/enable_if.hpp>
#include <vector>
#include <iostream>

namespace mpl = boost::mpl;

template<typename T,
typename = void>
struct Some
{
   typedef std::vector<T> type;
};

template<typename T>
struct Some<T, typename boost::enable_if_c<mpl::is_sequence<T>::type::value>::type> :
    public Some<typename mpl::front<T>::type>::type
{
};


int main()
{
   typedef mpl::vector<int, double> vect_t;
   typedef Some<vect_t> vector;
   vector vect;
   vect.push_back(1);
   std::cout << "int: " << vect.at(0) << std::endl;
}

http://liveworkspace.org/code/45d78872a2c7f30192277a81c655b471

MSVC说,push_backat不是Some<vect_t>的成员。

修改

开始,它看起来像MSVC 2012中的bug
#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/size.hpp>
#include <boost/utility/enable_if.hpp>
#include <vector>
#include <iostream>

namespace mpl = boost::mpl;

template<typename T, typename = void>
struct Some
{
    typedef std::vector<T> type;
};

template<typename T>
struct Some<T, typename boost::enable_if_c<mpl::is_sequence<T>::type::value>::type> :
    public std::vector<int>
{
};


int main()
{
   typedef mpl::vector<int, double> vect_t;
   typedef Some<vect_t>::type vector;
   vector vect;
   vect.push_back(1);
   std::cout << "int: " << vect.at(0) << std::endl;
}

给出错误,我无法push_back int进入std::vector<boost::mpl::vector<int, double> >,因此选择一般情况而非专业化......

修改

奇怪......但这可以按预期工作

template<typename T>
struct Some<T, typename std::enable_if<boost::mpl::is_sequence<T>::value>::type> :
    public std::vector<int>
{
};

所以,我无法解释原因,但MSVC 2012无法在enable_if(或可能在模板参数中)中使用嵌套表达式。

template<typename T>
struct is_int : public std::integral_constant<bool, false>
{
};

template<>
struct is_int<int> : public std::integral_constant<bool , true>
{
};

template<typename T, typename = void>
struct Some
{
    typedef void type;
};

template<typename T>
struct Some<T, typename std::enable_if<is_int<T>::type::value>::type>
{
    static_assert(is_int<int>::type::value, "asserted");
    typedef T type;
};

int main()
{
    static_assert(is_int<T>::type::value, "ass");
    Some<int>::type t = 0;
}

1 个答案:

答案 0 :(得分:1)

我在MSVC 2010中成功编译并运行您的代码,所以这可能是MSVC 2012的RC版本中的一个错误。所以在MSVC 2012 final中尝试它或者等待MSVC 2012 Express。