提升enable_if问题 [英] Boost enable_if problem

查看:82
本文介绍了提升enable_if问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从boost文档粘贴的副本,我不断收到错误消息(实际上有很多错误)

This is more or less copy pasted from boost docs and I keep getting an error (actually alot of errors)

我正在尝试确保模板类仅与使用boost的数字一起使用.这是一个增强的练习,而不是使模板类仅使用数字.

I'm trying to make sure that a template class is only used with numbers using boost. This is an exercise in boost, rather than making a template class that only uses numbers.

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>

using namespace boost;

template <class T>
class A<T, typename enable_if<is_arithmetic<T> >::type> // <-- this is line 9
{
    int foo;
};

int main() {

    return 0;
}

前几个错误C2143:语法错误:缺少';'在<"之前:第9行C2059:语法错误:'<':第9行C2899:类型名称不能在模板声明之外使用

The first few errors C2143: syntax error : missing ';' before '<' : line 9 C2059: syntax error : '<' : line 9 C2899: typename cannot be used outside a template declaration

Visual Studio 2005 btw.

Visual Studio 2005 btw.

推荐答案

您从未真正创建名为 A 的类模板.您刚刚创建了专业化.您首先需要使用虚拟参数创建 A 类模板,以使启动器正常工作.

You never actually created a class template called A. You just created a specialization. You need to first create the A class template with a dummy parameter for the enabler to work.

using namespace boost;

template <class T, class Enable = void>
class A { };

template <class T>
class A<T, typename enable_if<is_arithmetic<T> >::type> 
{
    int foo;
};

这篇关于提升enable_if问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆