看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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
CaptainH: const r-ref ...你確定? 08/16 11:05
ialk: 不太理解.. 抱歉0.0 08/16 11:16
ialk: 我拿掉const了 不過還是有link error 請問該怎麼實例化 08/16 11:27
Caesar08: 看到template<class T>...(const T&&),通常是寫錯了 08/16 13:09
ialk: 好的 我會再好好研讀這部分 08/16 13:46
bluesoul: 有試過const char *嗎? 08/16 19:21
ialk: 有的..可是我不知道怎麼改.. ((抱頭 08/16 20:04
bluesoul: 應該是你誤用了&&,改成&看看 08/16 20:56
ialk: 可是改成 void send_message_t(const T& msg); 08/17 10:39
ialk: 我該怎麼..實例化那個..const char* 08/17 10:40