我们可以添加一个基于模板参数的朋友类吗
Can we add a friend class based on template parameter?
我想知道以下棘手的情况是否可能:
假设我有一个模板类template <typename DTYPE> class A{};
,其中DTYPE
应该是uint8_t
、uint16_t
等中的一个。我想向A
添加一个朋友类,但这个朋友类对于每个DTYPE
备选方案都不同。此外,假设不同DTYPE
值的朋友类不是另一个模板类的实例化,而是独立的类。
有办法做到吗?
您可以添加模板"代理"类FriendOfA
,并将其专门化为您需要的任何类型:
// actual friends
class FriendUint8 {};
class FriendUint16 {};
template<typename T> struct FriendOfA;
template<>
struct FriendOfA<uint8_t> {
typedef FriendUint8 type;
};
template<>
struct FriendOfA<uint16_t> {
typedef FriendUint16 type;
};
// optional helper
template <typename T>
using FriendOfA_t = typename FriendOfA<T>::type;
template<class T>
class A {
friend typename FriendOfA<T>::type;
// or simply
friend FriendOfA_t<T>;
};
我相信你正在寻找这样的东西:
#include <iostream>
struct BaseFriend
{
template <typename T>
void boo(const T& t) { t.foo(); }
};
struct BaseFriendProxy
{
template <typename T>
void boo(const T& t) { std::cout << "Proxy: "; t.foo(); }
};
template <typename TType>
struct Friend ;
template <>
struct Friend<int> {
using T = BaseFriend;
};
template <>
struct Friend<char> {
using T = BaseFriendProxy;
};
template <typename DTYPE>
class A
{
private:
friend typename Friend<DTYPE>::T;
void foo() const
{ std::cout << "A::foo()" << std::endl; }
};
int main()
{
A<int> a;
BaseFriend bf1;
bf1.boo(a);
A<char> b;
BaseFriendProxy bf2;
bf2.boo(b);
return 0;
}
但这只适用于C++11:在C++03 中不能将friend class ...
和typename X::Y
组合起来
当然可以,专门化你的模板并添加你想要的任何朋友类:
#include <iostream>
using namespace std;
class base {
public:
virtual int getValue() = 0;
};
class friend1 {
public:
base* ptr;
int getValue() {
return ptr->getValue();
}
};
class friend2 {
public:
base* ptr;
int getValue() {
return ptr->getValue();
}
};
template <typename DTYPE> class A : public base{
public:
A() { data = 42; }
// No friends
private:
int data;
int getValue() {
return data;
}
};
template <> class A<char> : public base{
public:
A() { data = 44; }
friend class friend1;
private:
int data;
int getValue() {
return data;
}
};
template <> class A<bool> : public base{
public:
A() { data = 45; }
friend class friend2;
private:
int data;
int getValue() {
return data;
}
};
int main()
{
A<char> obj1;
friend1 friend_of_obj1;
friend_of_obj1.ptr = &obj1;
cout << friend_of_obj1.getValue() << endl;
A<bool> obj2;
friend2 friend_of_obj2;
friend_of_obj2.ptr = &obj2;
cout << friend_of_obj2.getValue();
}
http://ideone.com/hM9x0y
是的,朋友可以基于模板。例如
template <typename DTYPE>
struct Friend;
template <class DTYPE>
class A {
friend struct Friend<DTYPE>;
};
对于您希望支持的类A
的每种类型DTYPE
(根据实现不同),您可以专门处理Friend
,例如;
template<>
struct Friend<uint8_t> {
// ...
};
一个基本的工作样本:
#include <cstdint>
using namespace std;
template<typename T>
struct Friend;
template <class T>
class A {
friend struct Friend<T>;
int i = 0;
};
template<>
struct Friend<uint8_t> {
void method() {
A<uint8_t> a;
a.i = 8;
}
};
template<>
struct Friend<uint16_t> {
void method() {
A<uint16_t> a;
//A<uint8_t> b; // fails to compile
a.i = 16;
}
};
int main()
{
A<uint8_t> a;
Friend<uint8_t> f;
f.method();
}
相关文章:
- 何时提供默认参数作为模板参数
- 是否可以对零模板参数进行模板专门化
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 类成员函数参数列表是否可以依赖于模板参数?
- C++ 使函数调用依赖于模板参数
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 依赖于其他模板参数的模板参数
- 将 const 限定符应用于模板参数时的不同编译器行为
- 如何定义依赖于模板参数的类型定义
- C++ 错误:"没有依赖于模板参数的'setw'参数
- 依赖于模板参数的范围查找
- 使用的类型别名如何指定依赖于模板参数的模板模板参数
- G++:错误:没有依赖于模板参数的参数'pow',因此必须提供'pow'声明 [-fallowive]
- 嵌套在模板化类中的类的大小,但不依赖于模板参数
- 如何更好地处理依赖于模板参数的类成员类型
- 如何使用指向通用模板类的指针来访问依赖于模板参数类型的成员函数
- 如何解决 - 错误:没有依赖于模板参数的"退出"参数
- 类型定义应该依赖于模板参数
- 如何实现内部实现依赖于模板参数的类
- 专门化仅适用于带有enum非类型模板参数的c++模板函数