为什么模板不能在 extern “C"中?块?

2021-12-13 00:00:00 templates c++ extern linkage

这是对是否可以对指针进行 typedef 的答案-to-extern-C"-模板中的函数类型?

This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-"C"-function type within a template?

此代码无法使用 g++、Visual C/C++ 和 Comeau C/C++ 编译,错误消息基本相同:

This code fails to compile with g++, Visual C/C++, and Comeau C/C++ with basically the same error message:

#include <cstdlib>

extern "C" {
    static int do_stuff(int) {
        return 3;
    }

    template <typename return_t_, typename arg1_t_>
    struct test {
        static void foo(return_t_ (*)(arg1_t_)) { }
    };
}

int main()
{
    test<int, int>::foo(&do_stuff);
    return EXIT_SUCCESS;
}

g++ 说错误:带有 C 链接的模板",Visual C/C++ 发出编译器错误 C2894,Comeau C/C++ 说错误:此声明可能没有外部C"链接".

g++ says "error: template with C linkage", Visual C/C++ emits compiler error C2894, and Comeau C/C++ says "error: this declaration may not have extern "C" linkage".

事情是,所有人都很满意:

The thing is, all are happy with:

#include <cstdlib>

extern "C" {
    static int do_stuff(int) {
        return 3;
    }

    struct test {
        static void foo(int (*)(int)) { }
    };
}

int main()
{
    test::foo(&do_stuff);
    return EXIT_SUCCESS;
}

C++ 标准的第 7.5 节链接规范声明:

Section 7.5, Linkage specifications, of the C++ Standard states:

忽略类成员和成员函数的名称的 C 语言链接类成员函数的类型.

A C language linkage is ignored for the names of class members and the member function type of class member functions.

它甚至给出了例子:

extern "C" {
    class X {
        void mf(); // the name of the function mf and the member
                // function's type have C++ language linkage
        void mf2(void(*)()); // the name of the function mf2 has C++ language
                // linkage; the parameter has type pointer to C function
    };
}

如果在 extern "C" 块中允许模板,那么实例化的成员函数将具有 C++ 链接.

If templates were allowed in extern "C" blocks, then the member functions of the instantiations would have C++ linkage.

那么,为什么第 14 章,模板,C++98 标准状态:

Why, then, does chapter 14, Templates, of the C++98 Standard state:

模板名称可能有链接(3.5).模板、模板显式特化 (14.7.3) 和类模板部分特化不应具有 C 链接.

A template name may have linkage (3.5). A template, a template explicit specialization (14.7.3), and a class template partial specialization shall not have C linkage.

模板可能"具有链接是什么意思?什么是模板链接?

What does it mean that a template "may" have linkage? What is template linkage?

为什么明确禁止模板带有 C 链接,当一个类没有问题,并且模板实例化的所有成员函数(默认构造函数、析构函数和赋值运算符重载)都具有 C++ 链接时?

Why is it explicitly forbidden to have a template with C linkage, when a class is okay, and all member functions of instantiations of the template (the default constructor, destructor, and assignment operator overload) would have C++ linkage?

推荐答案

模板可能"具有链接是什么意思?什么是模板链接?

What does it mean that a template "may" have linkage? What is template linkage?

所有名称要么具有外部链接、内部链接,要么没有链接(C++03 §3.5p2),但这与语言链接不同.(令人困惑,我知道.C++0x 也通过链接改变了很多东西.)用作模板参数的任何东西都需要外部链接:

All names either have external linkage, internal linkage, or have no linkage (C++03 §3.5p2), but this is not the same linkage as language linkage. (Confusing, I know. C++0x changes things around considerably with linkage, too.) External linkage is required for anything used as a template argument:

void f() {
  struct S {};
  vector<S> v;  // Not allowed as S has internal linkage.
}

请注意,C++98 在您引用的 §14p4 中有可能",但 C++03 删除了可能",因为模板不能在会给它们提供内部链接的上下文中声明:

Notice that C++98 has "may" in what you quoted of §14p4, but C++03 removes the "may", as templates cannot be declared in a context that would give them internal linkage:

void f() {
  // Not allowed:
  template<class T>
  struct S {};
}

相关文章