我有代码,可以按预期编译和运行 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;
}
最佳答案
我在 MSVC 2010 中成功编译并运行了您的代码,所以这可能是 MSVC 2012 的 RC 版本中的错误。因此请在 MSVC 2012 final 中尝试它或等待 MSVC 2012 Express。
关于c++ - boost::enable_if MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12133390/