我有代码,可以按预期编译和运行,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;
}
给出错误,我不能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;
}