从基本对象获取派生类的模板参数

发布于 2025-01-21 02:07:36 字数 839 浏览 3 评论 0原文

简而言之:

  • 我有一个没有模板参数的基类 A_base 和一个有两个模板参数的派生类 A
  • 函数 foo() 只接受基类对象
  • foo() 应该返回一个与派生类的 first 模板参数 T。

这样做的原因是我不想将 foo() 专门用于 A 的所有变体,您可以想象可能有数百个变体。

为了在 代码 中说明这一点:

#include <cstdlib>

struct A_base
{

};

template <typename T, size_t S>
struct A : A_base
{
    using P = T; // <-- how do I get here from base class A?
    T arr[S];
};


template <typename T>
auto foo(const A_base& ref) -> decltype(/* somehow get P*/)
{
    return /* object of type P */;
}

int main()
{
    A<int, 10> a;

    foo(a);
}

我将另一个参数 P 键入到 T,因为我认为这将使类“拥有”该类型并提供更好的访问。我希望有一个没有虚拟方法的解决方案,因为到目前为止我一直在使用虚拟调用。

In short:

  • I have a base class A_base without template parameters and a derived class A that has two.
  • The function foo() only accepts base class objects
  • foo() is supposed to return an object which has the same type as the derived classes' first template parameter T.

The reason for this is that I don't want to specialize foo() for ALL variations of A which you can imagine could be hundreds.

To illustrate this in code:

#include <cstdlib>

struct A_base
{

};

template <typename T, size_t S>
struct A : A_base
{
    using P = T; // <-- how do I get here from base class A?
    T arr[S];
};


template <typename T>
auto foo(const A_base& ref) -> decltype(/* somehow get P*/)
{
    return /* object of type P */;
}

int main()
{
    A<int, 10> a;

    foo(a);
}

I typedefd another parameter P to T because I thought this would make the class "own" the type and provide for better access. I was hoping for a solution without virtual methods, because I got around using virtual calls until now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2025-01-28 02:07:36

编译器需要在编译时知道foo()的返回类型。如果您不能使A_Base成为模板,为什么不使FOO()本身成为函数模板?

如:

#include <type_traits>

struct A_base{};

template <typename T>
struct B : A_base
{
    static constexpr T val{};
};

template<typename T>
auto foo(const T&)
{
    static_assert(std::is_base_of_v<A_base, T>);  // this works without  
                                                        // depending on a particular
                                                        // constructor
    return T::val;  // for example.
}


struct C {};

int main()
{
    auto x = foo(B<int>{});
    return std::is_same_v<int, decltype(x)>;
}

The compiler needs to know the return type of foo() at compile time. If you cannot make A_base a template, why not make foo() itself a function template?

As in:

#include <type_traits>

struct A_base{};

template <typename T>
struct B : A_base
{
    static constexpr T val{};
};

template<typename T>
auto foo(const T&)
{
    static_assert(std::is_base_of_v<A_base, T>);  // this works without  
                                                        // depending on a particular
                                                        // constructor
    return T::val;  // for example.
}


struct C {};

int main()
{
    auto x = foo(B<int>{});
    return std::is_same_v<int, decltype(x)>;
}
孤独岁月 2025-01-28 02:07:36

很抱歉回答我自己的问题,但我发现了一种可能性。我只是创建一个中间虚拟类 A_middle,其中只有一个从 A 继承的模板参数 T。然后我可以使用这个中间类而不是 foo() 参数的基类:

#include <cstdlib>

struct A_base {};

template <typename T>
struct A_middle : A_base { };

template <typename T, size_t S>
struct A : A_middle<T>
{
    T arr[S];
};


template <typename T>
T foo(const A_middle<T>& ref) // deduction works as expected
{
    T var;
    return var;
}

int main()
{
    A<int, 10> b;

    foo(b);
}

Sorry for answering my own question but I found a possibility. I just create an intermediary dummy class A_middle with only one template parameter T that is inherited from A. I can then use this intermediary class and not the base class for foo()'s parameters:

#include <cstdlib>

struct A_base {};

template <typename T>
struct A_middle : A_base { };

template <typename T, size_t S>
struct A : A_middle<T>
{
    T arr[S];
};


template <typename T>
T foo(const A_middle<T>& ref) // deduction works as expected
{
    T var;
    return var;
}

int main()
{
    A<int, 10> b;

    foo(b);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文