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

將 const char* 作為模板參數傳遞

Passing const char* as template argument(將 const char* 作為模板參數傳遞)
本文介紹了將 const char* 作為模板參數傳遞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

為什么你不能在這里傳遞文字字符串?我通過一個非常小的解決方法使它工作.

Why can't you pass literal strings in here? I made it work with a very slight workaround.

template<const char* ptr> struct lols {
    lols() : i(ptr) {}
    std::string i;
};
class file {
public:
    static const char arg[];
};
decltype(file::arg) file::arg = __FILE__;
// Getting the right type declaration for this was irritating, so I C++0xed it.

int main() {
    // lols<__FILE__> hi; 
    // Error: A template argument may not reference a non-external entity
    lols<file::arg> hi; // Perfectly legal
    std::cout << hi.i;
    std::cin.ignore();
    std::cin.get();
}

推薦答案

因為這不是一個有用的實用程序.由于它們不是模板參數的允許形式,因此目前不起作用.

Because this would not be a useful utility. Since they are not of the allowed form of a template argument, it currently does not work.

讓我們假設它們有效.因為對于所使用的相同值,它們不需要具有相同的地址,所以即使您的代碼中具有相同的字符串文字值,您也會獲得不同的實例化.

Let's assume they work. Because they are not required to have the same address for the same value used, you will get different instantiations even though you have the same string literal value in your code.

lols<"A"> n;

// might fail because a different object address is passed as argument!
lols<"A"> n1 = n;

您可以為您的文本編輯器編寫一個插件,用逗號分隔的字符文字列表替換字符串并返回.使用可變參數模板,您可以以某種方式解決"這個問題.

You could write a plugin for your text editor that replaces a string by a comma separated list of character literals and back. With variadic templates, you could "solve" that problem this way, in some way.

這篇關于將 const char* 作為模板參數傳遞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 日韩电影中文字幕 | 亚洲一区中文字幕 | 丁香久久| 99精品电影 | 99精品国产一区二区青青牛奶 | 欧美精品一区久久 | 日韩综合在线 | 欧美日韩精品在线一区 | 欧美一级二级在线观看 | 免费午夜视频 | 精品二三区 | 亚洲日本一区二区 | 亚洲欧洲一区 | 亚洲高清免费 | 午夜精品久久久久久久星辰影院 | 瑟瑟激情| 国产日韩一区二区三免费 | 日韩欧美在线视频 | 成人福利在线观看 | 国产成人精品免费 | 亚洲国产精品一区二区三区 | 日韩av成人在线 | 国产欧美日韩综合精品一区二区 | 国产一区二区在线免费观看 | www九色 | 天天av网 | 国产亚洲精品久久yy50 | 国产美女一区二区 | 欧美一区二区三区在线 | 正在播放国产精品 | 在线看亚洲| 热久久999| h视频免费在线观看 | 免费观看黄网站 | 一区二区视频 | 日韩欧美高清 | 亚洲国产精品一区 | 国产精品日韩一区 | 亚洲黄色成人网 | 中文精品视频 | 蜜桃av一区二区三区 |