g++编译报错:specialization of template.... in different namespace解决方案

本文解决了一个在GCC编译器下遇到的关于std::hash模板特化的错误,该错误在Clang++中可以正常编译,但在GCC中却出现命名空间不匹配的问题。通过将特化代码放置在std命名空间内,成功解决了编译错误。

specialization of template… in different namespace解决方案

同样的代码,使用clang++编译通过,可是使用g++报错如下:
error: specialization of 'template<class _Tp> struct std::hash' in different namespace [-fpermissive] class std::hash<MyClassType>

源代码为:

template<>
class std::hash<MyClassType>
{	// hash functor
public:
	//...Codes...
};

修改为:

namespace std{
template<>
class hash<MyClassType>
{	// hash functor
public:
	//...Codes...
};
}

按照上面的改动就可以解决这个错误。看报错的原因,是说我声明的hash和系统预定义的hash不在同一个命名空间内。
在一些旧版的gcc中是可以接受这样的写法,但是这样是并不合法的。

这里的代码中的hash明明就是在std的命名空间。这个出错很奇怪,考虑可能是编译器的BUG。这个解决方案,也只是相同的代码逻辑换了一种写法而已。

参考链接:

  • https://blog.youkuaiyun.com/coder_xia/article/details/6764777
  • http://gcc.gnu.org/ml/gcc/2005-04/msg00134.html
  • https://womble.decadent.org.uk/c++/template-faq.html#specialise-ns
#ifndef H_6B9572DA_A64B_49E6_B234_051480991C89 #define H_6B9572DA_A64B_49E6_B234_051480991C89 #ifndef __cplusplus # error "It's not going to compile without a C++ compiler..." #endif #if defined(BACKWARD_CXX11) #elif defined(BACKWARD_CXX98) #else # if __cplusplus >= 201103L # define BACKWARD_CXX11 # define BACKWARD_ATLEAST_CXX11 # define BACKWARD_ATLEAST_CXX98 # else # define BACKWARD_CXX98 # define BACKWARD_ATLEAST_CXX98 # endif #endif // You can define one of the following (or leave it to the auto-detection): // // #define BACKWARD_SYSTEM_LINUX // - specialization for linux // // #define BACKWARD_SYSTEM_UNKNOWN // - placebo implementation, does nothing. // #if defined(BACKWARD_SYSTEM_LINUX) #elif defined(BACKWARD_SYSTEM_UNKNOWN) #else # if defined(__linux) # define BACKWARD_SYSTEM_LINUX # else # define BACKWARD_SYSTEM_UNKNOWN # endif #endif #include <fstream> #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <cctype> #include <string> #include <new> #include <iomanip> #include <vector> #if defined(BACKWARD_SYSTEM_LINUX) // On linux, backtrace can back-trace or "walk" the stack using the following // libraries: // // #define BACKWARD_HAS_UNWIND 1 // - unwind comes from libgcc, but I saw an equivalent inside clang itself. // - with unwind, the stacktrace is as accurate as it can possibly be, since // this is used by the C++ runtine in gcc/clang for stack unwinding on // exception. // - normally libgcc is already linked to your program by default. // // #define BACKWARD_HAS_BACKTRACE == 1 // - backtrace seems to be a little bit more portable than libunwind, but on // linux, it uses unwind anyway, but abstract away a tiny information that is // sadly really important in order to get perfectly accurate stack traces. // - backtrace is part of the (e)glib library. // // The default is: // #define BACKWARD_HAS_UNWIND == 1 // // Note that only one of the define should be set to 1 at a time. // # if BACKWARD_HAS_UNWIND == 1 # elif BACKWARD_HAS_BACKTRACE == 1 # else # undef BACKWARD_HAS_UNWIND # define BACKWARD_HAS_UNWIND 1 # undef BACKWARD_HAS_BACKTRACE # define BACKWARD_HAS_BACKTRACE 0 # endif // On linux, backward can extract detailed information about a stack trace // using one of the following libraries: // // #define BACKWARD_HAS_DW 1 // - libdw gives you the most juicy details out of your stack traces: // - object filename // - function name // - source filename // - line and column numbers // - source code snippet (assuming the file is accessible) // - variables name and values (if not optimized out) // - You need to link with the lib "dw": // - apt-get install libdw-dev // - g++/clang++ -ldw ... // // #define BACKWARD_HAS_BFD 1 // - With libbfd, you get a fair amount of details: // - object filename // - function name // - source filename // - line numbers // - source code snippet (assuming the file is accessible) // - You need to link with the lib "bfd": // - apt-get install binutils-dev // - g++/clang++ -lbfd ... // // #define BACKWARD_HAS_BACKTRACE_SYMBOL 1 // - backtrace provides minimal details for a stack trace: // - object filename // - function name // - backtrace is part of the (e)glib library. // // The default is: // #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1 // // Note that only one of the define should be set to 1 at a time. // # if BACKWARD_HAS_DW == 1 # elif BACKWARD_HAS_BFD == 1 # elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1 # else # undef BACKWARD_HAS_DW # define BACKWARD_HAS_DW 0 # undef BACKWARD_HAS_BFD # define BACKWARD_HAS_BFD 0 # undef BACKWARD_HAS_BACKTRACE_SYMBOL # define BACKWARD_HAS_BACKTRACE_SYMBOL 1 # endif # if BACKWARD_HAS_UNWIND == 1 # include <unwind.h> // while gcc's unwind.h defines something like that: // extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *); // extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *); // // clang's unwind.h defines something like this: // uintptr_t _Unwind_GetIP(struct _Unwind_Context* __context); // // Even if the _Unwind_GetIPInfo can be linked to, it is not declared, worse we // cannot just redeclare it because clang's unwind.h doesn't define _Unwind_Ptr // anyway. // // Luckily we can play on the fact that the guard macros have a different name: #ifdef __CLANG_UNWIND_H // In fact, this function still comes from libgcc (on my different linux boxes, // clang links against libgcc). # include <inttypes.h> extern "C" uintptr_t _Unwind_GetIPInfo(_Unwind_Context*, int*); #endif # endif # include <cxxabi.h> # include <fcntl.h> # include <link.h> # include <sys/stat.h> # include <syscall.h> # include <unistd.h> # include <signal.h> # if BACKWARD_HAS_BFD == 1 // NOTE: defining PACKAGE{,_VERSION} is required before including // bfd.h on some platforms, see also: // https://sourceware.org/bugzilla/show_bug.cgi?id=14243 # ifndef PACKAGE # define PACKAGE # endif # ifndef PACKAGE_VERSION # define PACKAGE_VERSION # endif # include <bfd.h> # ifndef _GNU_SOURCE # define _GNU_SOURCE # include <dlfcn.h> # undef _GNU_SOURCE # else # include <dlfcn.h> # endif # endif # if BACKWARD_HAS_DW == 1 # include <elfutils/libdw.h> # include <elfutils/libdwfl.h> # include <dwarf.h> # endif # if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1) // then we shall rely on backtrace # include <execinfo.h> # endif #endif // defined(BACKWARD_SYSTEM_LINUX) #ifdef BACKWARD_ATLEAST_CXX11 # include <unordered_map> # include <utility> // for std::swap namespace backward { namespace details { template <typename K, typename V> struct hashtable { typedef std::unordered_map<K, V> type; }; using std::move; } // namespace details } // namespace backward #else // NOT BACKWARD_ATLEAST_CXX11 # include <map> namespace backward { namespace details { template <typename K, typename V> struct hashtable { typedef std::map<K, V> type; }; template <typename T> const T& move(const T& v) { return v; } template <typename T> T& move(T& v) { return v; } } // namespace details } // namespace backward #endif // BACKWARD_ATLEAST_CXX11 namespace backward { namespace system_tag { struct linux_tag; // seems that I cannot call that "linux" because the name // is already defined... so I am adding _tag everywhere. struct unknown_tag; #if defined(BACKWARD_SYSTEM_LINUX) typedef linux_tag current_tag; #elif defined(BACKWARD_SYSTEM_UNKNOWN) typedef unknown_tag current_tag; #else # error "May I please get my system defines?" #endif } // namespace system_tag 注释上述代码
最新发布
09-29
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值