看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《ROGZ (ROGZ)》之銘言: 這種問題多半是不同STL版本問題 我個人對於這疑問的建議是,直接翻STL Source code。 翻STL這件事情沒那麼可怕,而且很多IDE都可以快速的幫你找到位置 比方說你的問題,大多數合格的IDE瞬間理解string要找到這裡 : template <class _CharT, class _Traits, class _Allocator> inline _LIBCPP_INLINE_VISIBILITY basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s) { _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr"); __init(__s, traits_type::length(__s)); #if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif } 也就是說其實他會對value_type去取長度(用traits_type::length) 這部分就先解決了你的問題:他怎麼知道const char*長度的 至於basic_string是什麼?string是basic_string的一個特殊具現化 但是你其實先不用知道,IDE都會幫你先處理掉 至於為什麼VC不行,我個人手邊是沒有windows不知道他STL怎麼實作的 不過你可以依樣畫狐狸的找一下能不能找到source code來討論? 以及你VC版本,以及他出了什麼error message? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.177.112.114 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1496324660.A.5D3.html
LPH66: 同問 VC++ 版本, 我手上的 2013 Community 沒這問題 06/01 22:02
LPH66: /FAsc 印出組語出來看也是呼叫了 string(char const *) 06/01 22:03
djshen: en.cppreference.com/w/cpp/language/copy_initialization 06/01 22:53
djshen: note部分 06/01 22:53
ROGZ: 謝謝您的解答 06/01 22:54
djshen: 另外 是VC可以吧? 06/01 22:54
ROGZ: 還有,VC是沒問題的, 是g++不行 06/01 22:55
Killercat: 我看反了...ok 加個-std=C++11在g++後面可以嗎? 06/01 23:22
Killercat: g++版本也看一下 我手邊只有clang,看不出問題 06/01 23:22