'template< class _Tp>的专业化struct std :: less'在不同的命名空间 [英] Specialization of 'template<class _Tp> struct std::less' in different namespace

查看:266
本文介绍了'template< class _Tp>的专业化struct std :: less'在不同的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



代码如下:

  template<> 
struct std :: less< DateTimeKey>
{
bool operator()(const DateTimeKey& k1,const DateTimeKey& k2)const
{
//一些代码...
}
}编译时(Ubuntu 9.10上的g ++ 4.4.1),我得到错误:



在不同的命名空间中'template struct std :: less'的专业化



我做了一些研究,发现有一个'workaround'涉及将专业化包装在std命名空间中 - 即将代码更改为:

 命名空间std {
模板<>
struct less< DateTimeKey>
{
bool operator()(const DateTimeKey& k1,const DateTimeKey& k2)const
{
//一些代码...
}
}
}

这确实会关闭编译器。然而,这个解决方案是从5年后(由'伟大的'维克多Bazarof不少[双关语无意的])。这个问题还有待解决,还是有更好的解决方法,或者是老方法仍然有效?

解决方案

这仍然是这样做的方式。不幸的是,你不能在命名空间中声明或定义函数,就像你对一个类所做的那样:你需要将它们包装在一个命名空间块中。


I am specializing the 'less' (predicate) for a data type.

The code looks like this:

template<>
struct std::less<DateTimeKey>
{
   bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
   {
      // Some code ...
   }
}

When compiling (g++ 4.4.1 on Ubuntu 9.10), I get the error:

Specialization of 'template struct std::less' in different namespace

I did some research and found that there was a 'workaround' which involved wrapping the specialization in a std namespace - i.e. changing the code to:

namespace std {
template<>
struct less<DateTimeKey>
{
   bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
   {
      // Some code ...
   }
}
}

which indeed, shuts the compiler up. However, that solution was from a post 5 years old (By the 'great' Victor Bazarof no less [pun unintended]). Is this fix still the way to go, or is there a better way of resolving this, or is the "old way" still valid?

解决方案

This is still the way to do it. Unfortunately you cannot declare or define functions within a namespace like you would do with a class: you need to actually wrap them in a namespace block.

这篇关于'template&lt; class _Tp&gt;的专业化struct std :: less'在不同的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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