对于具有两个模板化变量的模板化类,是否可以使一个 var 引用另一个 var
for a templated class with two templated variable, can one var be made to reference the other var?
我正在寻找一种从实际类型B和C中引用A's t的方法。在下面的代码中,你可以看到我的第一个倾向是尝试初始化它。我尝试过的其他尝试是使用完美的转发、继承和向 B 和 C 添加更多模板参数。有人可以提出前进的道路吗?是否有新的结构可能会有所帮助?我接近还是不可能?
struct D {};
struct E {};
template< typename U1 >
struct B
{
B() : u1(???)
U1& u1; // how to reference A's t variable?
};
template< typename U2 >
struct C
{
C() : u2(???)
U2& u2; // how to reference A's t variable?
};
template< typename T, typename U >
struct A
{
T t;
U u;
};
int main()
{
A< D, B< D > > a1;
A< E, C< E > > a2;
return 0;
}
我认为OP想要的是一个模板模板参数。
以下是他曾经审查过的代码:
#include<memory>
struct D {};
struct E {};
template< typename U >
struct B
{
B(std::shared_ptr<U> v) : u{v} {}
std::shared_ptr<U> u;
};
template< typename U >
struct C
{
C(std::shared_ptr<U> v) : u{v} {}
std::shared_ptr<U> u;
};
template< typename T, template<typename> typename U >
struct A
{
A(): t{std::make_shared<T>()}, u{t} {}
std::shared_ptr<T> t;
U<T> u;
};
int main()
{
A< D, B > a1;
A< E, C > a2;
return 0;
}
相关文章:
- 是否可以使C++类成为Objc类的委托
- 是否可以使一个类成为两个不同层次结构的子类?
- 是否有编译器标志可以使较新的 gcc 版本像旧版本一样构建
- C 可以使destuructor不称为班级成员和基类攻击方的灾难
- 可以使未命名的结构静态
- 是否可以使整数仅收到一个单个数字而不是两个接收输入
- C++:有哪些常规方法可以使代码更有效地用于大数字
- 是否可以使 std 容器使用默认运算符为新?
- LD_BIND_NOW可以使可执行文件运行得更慢?
- 是否可以使头文件使文本居中?- 在控制台中
- 有什么方法可以使核心忙碌等待
- 有没有一种技术可以使虚函数在所有派生类中强制重写?
- 有没有一种方法可以使全局函数/静态成员函数一次可呼出
- 是否有一种方法可以使此C 14递归模板在C 17中短
- 编写一个可以使二维数组平坦的函数
- 有哪些优化技巧可以使我的代码运行得更快
- 有没有更好的方法可以使此代码线程安全?线程局部静态似乎是一个生硬的工具
- 如果必须在同一向量上写入线程,是否可以使用线程
- 对于具有两个模板化变量的模板化类,是否可以使一个 var 引用另一个 var
- C 中的内容可以使Windows 8应用程序不在手臂上运行