問題描述
這是對是否可以對指針進行 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 {};
}
這篇關于為什么模板不能在 extern “C"中?塊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!