在成员函数中使用两个模板的类模板中为成员函数定义单个模板 [英] Defining a Single Template for a Member Function within a Class Template with Both Templates Used in Member Function

查看:60
本文介绍了在成员函数中使用两个模板的类模板中为成员函数定义单个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习模板在 C++ 中的工作原理.特别是,我正在查看类模板中的单个成员函数模板.要理解我的意思,请在下面找到代码.

I'm currently learning how templates work in C++. In particular, I'm looking at the single member function templates within class templates. To understand what I mean, code is found below.

// foo.h
template<typename A>
class foo {
    template<typename B>
    void boo(B);
};

// foo.cpp
template<typename A>
void foo<A>::boo(B value) {} // compiler error: 'Unknown' type name B

// or if I try this

template<typename B>
void foo<A>::boo(B value) {} // compiler error: Use of undeclared identifier A

我正在尝试使用两个类型名,一个来自类模板,一个来自单个文件模板,用于该特定功能.但是在上面的两个版本中,我遇到了编译器错误.我该如何绕过这个问题?

I'm trying to use two typenames, one from the class template, and one from the single file template, for that specific function. But in those two versions above, I get compiler errors. How would I bypass this problem?

推荐答案

这两组模板参数都需要定义成员模板.

Both the two sets of template parameters are required to define the member template.

(强调我的)

如果封闭类声明又是一个类模板,当在类主体之外定义成员模板时,它需要两组模板参数:一组用于封闭类,另一组用于本身:

If the enclosing class declaration is, in turn, a class template, when a member template is defined outside of the class body, it takes two sets of template parameters: one for the enclosing class, and another one for itself:

例如

template<typename A>
template<typename B>
void foo<A>::boo(B value) {} 

这篇关于在成员函数中使用两个模板的类模板中为成员函数定义单个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆