首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在专门化交换函数/时使用gcc8修复编译错误

如何在专门化交换函数/时使用gcc8修复编译错误
EN

Stack Overflow用户
提问于 2019-09-26 15:22:13
回答 2查看 284关注 0票数 1

我正在尝试编译代码,它专门为我的类编写函数std::swap。但是,我面临着一个问题,这是因为rvalue构造函数(当我评论行时,它可以工作)

我正在用g++ (GCC) 8.2.1 20180905 (RedHat8.2.1-3)和g++ -std=c++14 -Wall编写这些选项。

代码语言:javascript
复制
#include <iostream>
#include <utility>

namespace ns1
{
class Foo 
{
public:
    Foo();
    Foo(const Foo& that) {};
    Foo(Foo&& that) {};  // <--- work when commented
    ~Foo();  
    void swap(Foo &that) {};
};

inline void swap(Foo &lhs, Foo &rhs)
{
   lhs.swap(rhs);
}
}  // namespace ns1

namespace std {

template <>
inline void swap(ns1::Foo &lhs, ns1::Foo &rhs)
{
   lhs.swap(rhs);
}

} // namespace std

我有以下错误消息:

代码语言:javascript
复制
 error: template-id 'swap<>' for 'void std::swap(ns1::Foo&, ns1::Foo&)' does not match any template declaration
 inline void swap(ns1::Foo &lhs, ns1::Foo &rhs)
             ^~~~
In file included from /opt/rh/devtoolset-8/root/usr/include/c++/8/string:52,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/locale_classes.h:40,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/ios_base.h:41,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/ios:42,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/ostream:38,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/iostream:39,
                 from toto.cpp:1:
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:6276:5: note: candidates are: 'template<class _CharT, class _Traits, class _Alloc> void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&)'
     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~
In file included from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/stl_algobase.h:64,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/char_traits.h:39,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/ios:40,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/ostream:38,
                 from /opt/rh/devtoolset-8/root/usr/include/c++/8/iostream:39,
                 from toto.cpp:1:
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-26 15:59:03

您所看到的错误是,提供用户定义的移动构造函数阻止编译器合成移动赋值操作符,从而使类不可转让。根据中介人Tswap中必须是可移动的.

要修复它,还提供移动赋值操作符,演示

代码语言:javascript
复制
Foo& operator=(Foo&& other) {return *this;}
Foo& operator= (const Foo& other) { return *this; }

还请看一下五条规则

票数 1
EN

Stack Overflow用户

发布于 2019-09-26 15:51:57

也许,与其扩展std,不如使用ADL+using std::swap;

您的例子非常类似于这个准则。

(http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c165-use-using-for-customization-points)

另一条指导方针告诉我们,与模板专业化相比,我们更倾向于重载。

(http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#t144-dont-specialize-function-templates)

但不幸的是,它是std中未定义的行为。

(标准)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58120029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档