简体   繁体   English

尝试将C ++ 11代码转换为C ++ 03时,默认函数模板参数出错

[英]Error in default function template arguments when trying to convert C++11 code to C++03

I am trying to convert C++11 code to C++03 and stuck on a default template argument. 我试图将C ++ 11代码转换为C ++ 03并停留在默认模板参数上。

#include <type_traits>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/spirit/home/support/string_traits.hpp>

template<bool B, class T = void>
struct enable_if {};

template<class T>
struct enable_if<true, T> { typedef T type; };

template<typename T>
struct is_char
{
    typedef typename  enable_if<sizeof (T) == sizeof (char)>::type eif;
};


template<bool B, class T, class F>
struct conditional { typedef T type; };

template<class T, class F>
struct conditional<false, T, F> { typedef F type; };

template <typename ObjType,
        typename PtrType,
        typename CharType =
            typename conditional<boost::is_const<PtrType>::value,
                                      const typename ObjType::char_type,
                                      typename ObjType::char_type>::type,
        typename is_char<PtrType>::type >
CharType* char_ptr_cast(PtrType* p)
{ return reinterpret_cast<CharType*>(p); }

int main ()
{}

I am getting following error: 我收到以下错误:

> /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/c++0x_warning.h:31:2:
> error: #error This file requires compiler and library support for the
> upcoming ISO C++ standard, C++0x. This support is currently
> experimental, and must be enabled with the -std=c++0x or -std=gnu++0x
> compiler options. 
> 
> test.cc:35: error: no default argument for anonymous
> 
> **default template arguments may not be used in function templates without -std=c++0x or -std=gnu++0x**

Could you please help me to resolve these errors? 你能帮我解决一下这些错误吗?

Default template arguments for function templates were added in C++11. 在C ++ 11中添加了函数模板的默认模板参数。 If you can't use C++11, or your compiler doesn't support it properly, you can't define typename CharType = /* whatever */ . 如果您不能使用C ++ 11,或者您的编译器不能正确支持它,则无法定义typename CharType = /* whatever */ The way towards C++03 compliance without retyping that long meta-function is to refactor CharType into its own trait, and use that. 对C ++ 03合规的方式,无需重新键入,长期元功能是重构CharType到其自己的特点,并使用它。

template<typename ObjType, typename PtrType>
struct CharType {
    typedef typename conditional<boost::is_const<PtrType>::value,
                                 const typename ObjType::char_type,
                                 typename ObjType::char_type>::type
    type;
};

template <typename ObjType typename PtrType>
typename CharType<ObjType, PtrType>::type* char_ptr_cast(PtrType* p)
{ return reinterpret_cast<typename CharType<ObjType, PtrType>::type*>(p); }

Also, the <type_traits> header is a C++11 only header. 此外, <type_traits>标头是仅限C ++ 11的标头。 Since you hit a #error directive following a failed standard's version check in the standard library, that is the most likely culprit. 由于您在标准库中检查失败的标准版本后遇到了#error指令,因此这是最可能的罪魁祸首。 You cannot include it. 你不能包括它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM