为什么我不能在 C++ 中使用模板化 typedef?

发布于 2024-10-21 02:10:35 字数 1599 浏览 1 评论 0原文

考虑以下程序:

#include <iostream>
#include <algorithm>

using namespace std;

template<class T>
struct A {
    typedef pair<T, T> PairType;
};

template<class T>
struct B {
    void f(A<T>::PairType p) {
        cout << "f(" << p.first << ", " << p.second << ")" << endl;
    }
    void g(pair<T, T> p) {
        cout <<"g(" << p.first << ", " << p.second << ")" << endl;
    }
};

int main() {
    B<int> b;
    b.f(make_pair(1, 2));
    b.g(make_pair(1, 2));
}

为什么它不能编译?它抱怨 B::f() 方法的部分。它似乎无法识别 A 类中的 typedef。如果我将 T 更改为具体类型,它仍然可以工作。完整的错误消息如下:

g++ -DNDEBUG -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp:13: error: ‘template<class T> struct A’ used without template parameters
../main.cpp:13: error: expected ‘,’ or ‘...’ before ‘p’
../main.cpp: In member function ‘void B<T>::f(int)’:
../main.cpp:14: error: ‘p’ was not declared in this scope
../main.cpp: In function ‘int main()’:
../main.cpp:23: error: no matching function for call to ‘B<int>::f(std::pair<int, int>)’
../main.cpp:13: note: candidates are: void B<T>::f(int) [with T = int]
make: *** [main.o] Error 1

我什至尝试了另一种方式,但它仍然不起作用:

void f(A::PairType<T> p) {
    cout << "f(" << p.first << ", " << p.second << ")" << endl;
}

如何使这样的代码起作用?

Consider the following program:

#include <iostream>
#include <algorithm>

using namespace std;

template<class T>
struct A {
    typedef pair<T, T> PairType;
};

template<class T>
struct B {
    void f(A<T>::PairType p) {
        cout << "f(" << p.first << ", " << p.second << ")" << endl;
    }
    void g(pair<T, T> p) {
        cout <<"g(" << p.first << ", " << p.second << ")" << endl;
    }
};

int main() {
    B<int> b;
    b.f(make_pair(1, 2));
    b.g(make_pair(1, 2));
}

Why doesn't it compile? It complains about the part with the B::f() method. It doesn't seem to recognize the typedef in class A<T>. If I change T to a concrete type, it works though. The full error message is the following:

g++ -DNDEBUG -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp:13: error: ‘template<class T> struct A’ used without template parameters
../main.cpp:13: error: expected ‘,’ or ‘...’ before ‘p’
../main.cpp: In member function ‘void B<T>::f(int)’:
../main.cpp:14: error: ‘p’ was not declared in this scope
../main.cpp: In function ‘int main()’:
../main.cpp:23: error: no matching function for call to ‘B<int>::f(std::pair<int, int>)’
../main.cpp:13: note: candidates are: void B<T>::f(int) [with T = int]
make: *** [main.o] Error 1

I even tried it another way, but it still didn't work:

void f(A::PairType<T> p) {
    cout << "f(" << p.first << ", " << p.second << ")" << endl;
}

How could such code be made to work?

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

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

发布评论

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

评论(1

许你一世情深 2024-10-28 02:10:35

解析 struct B 模板时,编译器不知道 A::PairType 是一种类型。了解 A::PairType 是否为类型的唯一方法是实例化两个模板,这在 main 函数之前不会发生。

明确地告诉编译器它是这样的:

void f(typename A<T>::PairType p)

The compiler doesn't know that A<T>::PairType is a type when parsing struct B template. The only way of knowing whether A<T>::PairType is a type or not is instantiating both templates, which does not happen until your main function.

Tell the compiler explicitly that it is so:

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