Re: [问题] G++ and VC++ Constructor 问题

楼主: Killercat (杀人猫™)   2017-06-01 21:44:18
※ 引述《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?
作者: LPH66 (-6.2598534e+18f)   2017-06-01 22:02:00
同问 VC++ 版本, 我手上的 2013 Community 没这问题/FAsc 印出组语出来看也是呼叫了 string(char const *)
作者: djshen (djshen)   2017-06-01 22:53:00
en.cppreference.com/w/cpp/language/copy_initializationnote部分
作者: ROGZ (ROGZ)   2017-06-01 22:54:00
谢谢您的解答
作者: djshen (djshen)   2017-06-01 22:54:00
另外 是VC可以吧?
作者: ROGZ (ROGZ)   2017-06-01 22:55:00
还有,VC是没问题的, 是g++不行
楼主: Killercat (杀人猫™)   2017-06-01 23:22:00
我看反了...ok 加个-std=C++11在g++后面可以吗?g++版本也看一下 我手边只有clang,看不出问题

Links booklink

Contact Us: admin [ a t ] ucptt.com