为什么我使用这些意大利面模板会"error: type name is not allowed"?

Why am I getting "error: type name is not allowed" with these spaghetti templates?

本文关键字:type error name is allowed not 意大利 为什么      更新时间:2023-10-16

我有以下代码,带有一些模板的"意大利面条":

template <typename A, typename B, typename... Args>
class C {
    /*... */
    Bar& getBar() { /* ... */ }
public:
    template <typename U>
    static void Foo() {
        A a = getA<U>();
        Baz& bar = getBar();
        bar.frob<U>(a); /* @@@ */
    }
    /*... */
}
/* no template */
class D : public C<SomeA, D, const SomeE&> { /* ... */ }
/* no template */
class F : public D { /* ... */}

当我尝试使用以下语句编译函数时:

D::Foo<F>();

我在标记为 @@@ 的行上收到错误type name is not allowed。我为什么会得到它?我知道当你尝试调用带有类型名称的函数时你会得到它,但它看起来不像我在这里这样做。

这是一个经典的模板歧义,不幸的是,如果您不了解它,很难注意到它。答:你需要写bar.template frob<U>(a);原因:看这个问答

相关文章: