- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这个有效:
template<class Tim>
struct Bob
{
struct Dave
{
Tim t{};
friend bool operator < (const Dave& a, const Dave& b)
{
return a.t < b.t;
}
} d;
};
这不起作用:
template<class Tim>
struct Bob
{
struct Dave
{
Tim t{};
friend bool operator < (const Dave& a, const Dave& b);
} d;
};
template<class Tim>
bool operator < (const typename Bob<Tim>::Dave& a, const typename Bob<Tim>::Dave& b)
{
return a.t < b.t;
}
例如,当我尝试在 map 中使用它时,出现链接器错误:
1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol "bool __cdecl operator<(struct Bob<int>::Dave const &,struct Bob<int>::Dave const &)" (??M@YA_NABUDave@?$Bob@H@@0@Z) referenced in function "public: bool __thiscall std::less<struct Bob<int>::Dave>::operator()(struct Bob<int>::Dave const &,struct Bob<int>::Dave const &)const " (??R?$less@UDave@?$Bob@H@@@std@@QBE_NABUDave@?$Bob@H@@0@Z)
.
int main()
{
std::map<Bob<int>::Dave, int> v;
v[{}];
}
如何在类之外正确定义此运算符?
最佳答案
您通常会通过前向声明模板类和友元函数,然后在类定义中提供特化来做这样的事情。然而,在这种情况下它并不那么容易 - 具有依赖类型将 Tim
在非推导上下文中上课,因此推导将失败。但是,有一种解决方法:
#include <iostream>
#include <type_traits>
#include <map>
template<class T>
struct Bob;
template<typename T, typename>
bool operator < (const T& a, const T& b);
struct DaveTag {};
template<class Tim>
struct Bob
{
struct Dave : DaveTag
{
Tim t{};
friend bool operator < <Bob<Tim>::Dave, void>(const typename Bob<Tim>::Dave& a, const typename Bob<Tim>::Dave& b);
} d;
};
template<typename T, typename = typename std::enable_if<std::is_base_of<DaveTag, T>::value>::type>
bool operator < (const T& a, const T& b)
{
return a.t < b.t;
}
struct X {
double t;
};
int main()
{
std::map<Bob<int>::Dave, int> v;
v[{}];
// This won't work
// X x, y;
//bool b = x < y;
}
基本上,我在这里所做的是让编译器推导出完整的 Bob<Tim>::Dave
作为 operator<
的模板参数.然而,很明显,一个简单的定义将允许为 T
推导任何类型。可能会导致一些难以理解的问题。为了避免它,我添加了一个小标签类 DaveTag
这允许防止我们非常通用的实例化 operator<
除了Dave
.
关于c++ - 如何在模板类的嵌套类中提供友元运算符的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059924/
在 C++ 中,我使用多态类和友元来创建一个基本的“ friend 组”。但是,当我尝试访问类人(男孩类的 friend )的私有(private)年龄函数时,我无法访问它。有什么问题吗? /* Po
当一个 lambda 函数在 C 类的友元函数 F 中声明时,该 lambda 函数是否可以访问 C私有(private)成员(member)?具体来说,标准允许吗? 最佳答案 C++11 §[exp
首先回顾以前所学的构造函数 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在定义时被自动调用 由于构造函数没有返回值不能判断执行结果,所以不能保证初始
我想要一个模板化的友元函数。但是,我不知道如何使它在没有模板化功能的情况下以相同的方式工作。这是一个示例代码 #include namespace ns{ struct Obj { frie
我目前正在创建一个实用程序类,其中将包含重载的运算符。使它们成为成员或非成员 (friend) 函数的优缺点是什么?还是根本不重要?也许对此有最佳实践? 最佳答案 我会选择“C++ 编码标准:101
template struct test { template friend struct test; }; int main() {} 这是完全有效的代码,不是吗?我问是因为 MSVC+
相对于Java而言,友元是C++中特有的一种元素,很多教材上对其介绍的相对较少,因此初学的时候往往不能很快掌握,本文总结了友元的用法和一些注意的地方,供大家参考借鉴。希望能对初学C++的朋友起到一点
类中声明的友元函数和公共(public)函数有什么区别?当我们可以将好友功能公开并具有相同的访问权限时,使用好友功能还有什么意义呢? 最佳答案 public 指类成员函数的访问级别。 成员函数可以是公
如何在所有派生类中没有 friend 的情况下访问 privateMember? class parent{...}; //a virtual class class A: public paren
我是初学者级别的面向对象编程爱好者。我遇到了以下难题: class A { }; class B { protected: friend class A; }; class C { publi
前言:这些知识点属于C++较为前期的内容,博主在今年刷笔试题的时候遇到多次,所以特地这这篇博客再复习了一下。 1.构造函数的初始化 在推导之前,关于初始化我们先达成一点共识:初始化只能一次(记住这点)
大家好, 我正在尝试为我的数学 vector 类重载加法运算符。我的(看似逻辑正确的)简化代码是: template class Vector2 { private: T m_da
我是一名优秀的程序员,十分优秀!