将模板函数作为参数而没有实例化?

发布于 2025-02-12 10:14:40 字数 1165 浏览 0 评论 0原文

我一直在C ++进行一些练习,偶然发现了一个练习,这是关于创建一个简单的模板函数迭代器,该函数迭代器采用任何类型的数组和其他模板功能来在数组中的每个元素中进行一些处理,我已经完成了看起来如下:

template <typename T >
void    increment(T& t)  {
        t += 1;
}

template <typename T, typename F>
void    iterate(T *arr, size_t size, F f) {
        for (size_t i = 0; i < size; i++) f(arr[i]);
}

int main( void )  {

        int tab[]= {0,1,2,3,4};
        SomeClass tab2[5];
        iterate(tab, 5, increment<int>);
        iterate(tab2, 5, increment<SomeClass>);
        return (0);
}

主要功能中的那两个测试是我写我自我的两个测试,现在练习也提供了一些测试以检查解决方案,并且测试几乎相同,它使用了一个简单的测试print()模板函数而不是rezement()

    template <typename T >
    void    print(const T& t) {std::cout << t << " "; };

现在的问题是,它使用以下实例化模板函数ITERATE()

    iterate(tab, 5, print);
    iterate(tab2, 5, print);

现在根据对于我的理解,您不能在没有实例化的情况下将模板函数作为参数传递,因为它不是常规函数,应该对其进行实例化,否则编译器不知道为实际函数创建哪种类型?的确,当我编写练习的测试时,我得到了一个:

no matching function for call to 'iterate'

所以我的问题是正确的,这只是练习中的一个错误,或者测试是正确的,而我是缺少某些东西的人?

I've been doing some exercises in C++ and stumbled upon one, it was about creating a simple a templated function iterator that takes an array of any type and another templated function to do some processing in each element in the array, i've completed the exerisce to look like the following:

template <typename T >
void    increment(T& t)  {
        t += 1;
}

template <typename T, typename F>
void    iterate(T *arr, size_t size, F f) {
        for (size_t i = 0; i < size; i++) f(arr[i]);
}

int main( void )  {

        int tab[]= {0,1,2,3,4};
        SomeClass tab2[5];
        iterate(tab, 5, increment<int>);
        iterate(tab2, 5, increment<SomeClass>);
        return (0);
}

Those 2 tests in the main function is those which i wrote my self, now the exercise too provide some tests to check the solution, and it's tests almost the same, it uses a simple print() templated function instead of increment()

    template <typename T >
    void    print(const T& t) {std::cout << t << " "; };

Now the problem here is it uses the following instantiation for templated function iterate():

    iterate(tab, 5, print);
    iterate(tab2, 5, print);

Now according to what my understanding you cannot pass a templated function as argument without instantiation because it's not a regular function, it should be instantiated otherwise the compiler wouldn't know what type to create for the actual function ?? and indeed when i compiled the exercise's tests i got a:

no matching function for call to 'iterate'

so my question is am i correct and this is just a mistake in the exercise or the tests are correct and i'm the one who's missing something ?

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

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

发布评论

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

评论(2

把梦留给海 2025-02-19 10:14:44

是的,你是正确的。函数模板不能作为模板参数传递,因为它还没有类型。

您可以通过的是函子,要么定制一个带有重载operator()或仅是lambda的函数。您确定措辞不会谈论函数吗?

以下代码有效:

auto print = [](const auto& t) {std::cout << t << " "; };
iterate(tab, 5, print);
iterate(tab2, 5, print);

这也是许多STL算法采用的方法。

Yes, you are correct. A function template cannot be passed as a template argument because it does not have a type yet.

What you can pass is a functor, either custom one with overloaded operator() or just a lambda. Are you sure the wording does not talk about functors?

The following code works:

auto print = [](const auto& t) {std::cout << t << " "; };
iterate(tab, 5, print);
iterate(tab2, 5, print);

This is also the approach many STL algorithms took.

吝吻 2025-02-19 10:14:44

任务可能希望您使用功能指针。在此中,可以在某些情况下推导打印的参数类型。请注意,如果您要同时修改和非修改功能(=函数为const引用)工作,则需要对模板的模板参数进行签名,该签名可能匹配这两种模板,例如您的增量模板。

template <typename T >
void increment(T& t)
{
    t += 1;
}

template <typename T>
void iterate(T* arr, size_t size, void (*f)(const T&)) {
    for (size_t i = 0; i < size; i++) f(arr[i]);
}

template <typename T>
void    iterate(T* arr, size_t size, void(*f)(T&)) {
    for (size_t i = 0; i < size; i++) f(arr[i]);
}

template <typename T >
void    print(const T& t) { std::cout << t << " "; };


int main(void) {
    int tab[] = { 0,1,2,3,4 };
    iterate(tab, 5, increment<int>); // not possible to deduce the template parameter for increment here (both const int and int would be signatures matching an overload of iterate)
    iterate(tab, 5, print); // template parameter of print deduced as int
}

The task may be expecting you to use function pointers. In this the parameter type of print can be deduced in some scenarios. Note that if you want both modifying and non-modifying functions (=functions taking a reference to const) to work, you need to pecify the template parameter for templates with a signature that could match both, e.g. your increment template.

template <typename T >
void increment(T& t)
{
    t += 1;
}

template <typename T>
void iterate(T* arr, size_t size, void (*f)(const T&)) {
    for (size_t i = 0; i < size; i++) f(arr[i]);
}

template <typename T>
void    iterate(T* arr, size_t size, void(*f)(T&)) {
    for (size_t i = 0; i < size; i++) f(arr[i]);
}

template <typename T >
void    print(const T& t) { std::cout << t << " "; };


int main(void) {
    int tab[] = { 0,1,2,3,4 };
    iterate(tab, 5, increment<int>); // not possible to deduce the template parameter for increment here (both const int and int would be signatures matching an overload of iterate)
    iterate(tab, 5, print); // template parameter of print deduced as int
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文