gpt4 book ai didi

c++ - 将依赖的模板类加为好友

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:31 25 4
gpt4 key购买 nike

我的代码中有一个案例,我需要将依赖模板类作为 friend ,但我认为我已经用尽了所有可能性,但没有一个可行。这有可能做到吗?如果是,怎么办?

最简单的例子:

struct Traits {
template <typename T>
struct Foo {
typedef typename T::bar* type;
};
};

template <typename T>
class Bar
{
typedef int bar;
// need to make T::Foo<Bar> a friend somehow?
typedef typename T::template Foo<Bar>::type type; // won't compile because bar is private...
// suppose I cannot make bar public.


type val;
};

int main() {
Bar<Traits> b;
}

最佳答案

struct Traits {
template <typename T>
struct Foo {
typedef typename T::bar* type;
};
};

template <typename T>
class Bar
{
typedef int bar;
friend typename T::template Foo<Bar>;
typedef typename T::template Foo<Bar>::type type;
type val;
};

int main() {
Bar<Traits> b;
}

关于c++ - 将依赖的模板类加为好友,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com