简体   繁体   中英

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.

#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. If you can't use C++11, or your compiler doesn't support it properly, you can't define 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.

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. Since you hit a #error directive following a failed standard's version check in the standard library, that is the most likely culprit. You cannot include it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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