[英]boost::enable_if MSVC
我有代码,可以在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_back
和at
不是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;
}
给出错误,我无法将int
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.