[問題] explicit instantiation的問題

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

Links booklink

Contact Us: admin [ a t ] ucptt.com