[問題] explicit instantiation的問題

看板C_and_CPP作者 (沒有靈感..)時間7年前 (2016/08/16 10:35), 編輯推噓3(307)
留言10則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) MSVC14 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 原本的code是一個負責傳遞文字訊息class收到std::string或std::wstring之後 將文字訊息傳遞出去 所以在.h裡面宣告了 void send_message(const std::string& msg); void send_message(const std::wstring& msg); 我想將這個function改寫成template 所以我寫了 template<typename T> void send_message_t(const T&& msg); 在cpp裡做explicit instantiation template void MyClass::send_message_t<std::string>(const std::string&&); template void MyClass::send_message_t<std::wstring>(const std::wstring&&); 原有的function由於const char[]會implicit conversion成std::string所以當呼叫 send_message("message"); 的時候是沒有問題的 但是由於我做了explicit instantiation 目前只能寫成 send_message_t(std::string("message")); 我想我應該將const char[]也做explicit instantiation 可是不知道該怎麼寫.. 有說錯的地方請指教 謝謝 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): cannot convert argument 1 from const char[8] to const char(&&)[8]; 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.69.170.37 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1471314914.A.89B.html

08/16 11:05, , 1F
const r-ref ...你確定?
08/16 11:05, 1F

08/16 11:16, , 2F
不太理解.. 抱歉0.0
08/16 11:16, 2F

08/16 11:27, , 3F
我拿掉const了 不過還是有link error 請問該怎麼實例化
08/16 11:27, 3F

08/16 13:09, , 4F
看到template<class T>...(const T&&),通常是寫錯了
08/16 13:09, 4F

08/16 13:46, , 5F
好的 我會再好好研讀這部分
08/16 13:46, 5F

08/16 19:21, , 6F
有試過const char *嗎?
08/16 19:21, 6F

08/16 20:04, , 7F
有的..可是我不知道怎麼改.. ((抱頭
08/16 20:04, 7F

08/16 20:56, , 8F
應該是你誤用了&&,改成&看看
08/16 20:56, 8F

08/17 10:39, , 9F
可是改成 void send_message_t(const T& msg);
08/17 10:39, 9F

08/17 10:40, , 10F
我該怎麼..實例化那個..const char*
08/17 10:40, 10F
文章代碼(AID): #1NidlYYR (C_and_CPP)