久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么模板不能在 extern “C"中?塊?

Why can#39;t templates be within extern quot;Cquot; blocks?(為什么模板不能在 extern “C中?塊?)
本文介紹了為什么模板不能在 extern “C"中?塊?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

這是對是否可以對指針進行 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 免费观看一级特黄欧美大片 | 成人精品久久久 | 欧美一区免费 | 成人精品一区 | 狠狠色综合欧美激情 | 最新国产精品精品视频 | 三级免费毛片 | 天天操,夜夜爽 | 网黄在线| 天天综合久久 | 伊人网国产 | 91传媒在线观看 | 国产福利资源在线 | 日韩精品成人 | 成人国产综合 | 久久久性色精品国产免费观看 | 美女福利视频 | 精品少妇v888av | 欧美精品在线免费观看 | 国产欧美日韩在线一区 | 精品国产欧美日韩不卡在线观看 | pacopacomama在线| 538在线精品 | 九一在线| 国产在线不卡视频 | 日韩精品一区在线观看 | 欧美日韩视频一区二区 | 欧美一级免费看 | 亚洲精品国产第一综合99久久 | 国产婷婷色综合av蜜臀av | 国产区免费视频 | 色婷婷亚洲 | 91新视频| 精品香蕉一区二区三区 | 国产欧美一级二级三级在线视频 | 日本电影韩国电影免费观看 | 99精品国产一区二区三区 | 亚洲综合婷婷 | 亚洲综合在线一区 | 天天看逼| 中文字幕久久精品 |