c++ - boost::enable_if MSVC

标签 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 中的错误

#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;
}

最佳答案

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

关于c++ - boost::enable_if MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12133390/

相关文章:

c++ - 在包含处理程序的类被破坏后,Asio 调用处理程序

python - 使用 bjam 的 pythonpath 功能

c++ - 在 Linux 上将文件写入磁盘的最快方法

c++ - 为什么代码在 CodeBlocks 上不起作用,但在 VS 上有效

c++ - 对输入的数字进行排序 - FOR 和 WHILE 之间的区别

C++ struct 默认构造函数行为

c++ - 接受对象作为参数的构造函数发生了什么?

c++ - 当我尝试写入二维数组时出现未处理的异常

azure - 在 azure 上部署速度非常慢 : 'Warning: Unable to connect to the remote server'

visual-studio-2012 - 如何防止Visual Studio 2012自动关闭预览文件?