专门化模板功能返回向量

发布于 2025-01-23 10:11:14 字数 566 浏览 2 评论 0原文

假设我在文件上有一个读者类:

class Reader {
public:
    template <class T>
    T Read();
};

它的唯一函数是读取函数,读取任何算术类型(static_assert(std :: is_arithmetic_v&lt; t&gt; t&gt;))文件。现在,我想创建该功能的专业化,该功能从文件中读取向量。我将如何使用模板进行操作?像以下内容不起作用:

template <class T>
std::vector<T> Read<std::vector<T>>();
error: function template partial specialization is not allowed
std::vector<U> Read<std::vector<U>>();
               ^   ~~~~~~~~~~~~~~~~

Let's say I have a reader class over a file:

class Reader {
public:
    template <class T>
    T Read();
};

Its only function is the Read function that reads any arithmetic type (static_assert(std::is_arithmetic_v<T>)) from a file. Now I want to create a specialization of that function, which reads a vector from the file. How would I go about doing that with templates? Something like the following doesn't work:

template <class T>
std::vector<T> Read<std::vector<T>>();
error: function template partial specialization is not allowed
std::vector<U> Read<std::vector<U>>();
               ^   ~~~~~~~~~~~~~~~~

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

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

发布评论

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

评论(2

标点 2025-01-30 10:11:14

您不能部分地专业化功能。不过,您可以超载它们,但是这样做的方式并不明显,因为您的功能没有任何参数。

首先,您需要一种方法来检查一种类型是否为std :: vector&lt; ??&gt;

template <typename T> struct IsVector : std::false_type {};
template <typename ...P> struct IsVector<std::vector<P...>> : std::true_type {};

然后,您可以将其插入需要

template <typename T>
T Read()
{
    // Generic overload
}

template <typename T> requires IsVector<T>::value
T Read()
{
    // Vector overload
}

另外,您可以拥有一个单个功能,具有,如果constexpr(ISVector&lt; t&gt; :: value)内部。

You can't partially specialize functions. You can overload them though, but the way of doing it is not obvious, since your function doesn't take any parameters.

First, you need a way to check if a type is a std::vector<??>:

template <typename T> struct IsVector : std::false_type {};
template <typename ...P> struct IsVector<std::vector<P...>> : std::true_type {};

Then you can plug it into requires:

template <typename T>
T Read()
{
    // Generic overload
}

template <typename T> requires IsVector<T>::value
T Read()
{
    // Vector overload
}

Alternatively, you could have a single function, with if constexpr (IsVector<T>::value) inside.

梦里梦着梦中梦 2025-01-30 10:11:14

实施您想要的方法是将成员函数的逻辑委托给几个私人成员函数:

#include <cstdio>
#include <vector>

class BinaryReader {
   public:
    template <class T>
    T Read() {
        T t{};
        this->ReadImpl(t);
        return t;
    }

private:
    template <class T>
    void ReadImpl(T& t) {
        static_assert(std::is_arithmetic_v<T>);
        std::puts("T");
        t = T{}; // implement your logic here
    }

    template <class T>
    void ReadImpl(std::vector<T>& t) {
        std::puts("std::vector<T>");
        t = std::vector<T>{}; // implement your logic here
    }
};

int main() {
    BinaryReader br;
    br.Read<int>();
    br.Read<std::vector<int>>();
}

这不需要您引入新的类型特征以检查您的类型是否为std :: vector&lt; &gt;。但是,它要求您的返回类型默认可构建。

输出:

T
std::vector<T>

A way to implement what you want is to delegate the logic of your member function to a couple of private member functions:

#include <cstdio>
#include <vector>

class BinaryReader {
   public:
    template <class T>
    T Read() {
        T t{};
        this->ReadImpl(t);
        return t;
    }

private:
    template <class T>
    void ReadImpl(T& t) {
        static_assert(std::is_arithmetic_v<T>);
        std::puts("T");
        t = T{}; // implement your logic here
    }

    template <class T>
    void ReadImpl(std::vector<T>& t) {
        std::puts("std::vector<T>");
        t = std::vector<T>{}; // implement your logic here
    }
};

int main() {
    BinaryReader br;
    br.Read<int>();
    br.Read<std::vector<int>>();
}

This doesn't require you to introduce new type traits to check if your type is a std::vector<>. However, it requires your return types to be default constructible.

Output:

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