我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
以下代码在gcc 4.8和Clang 3.2下编译:
int main()
{
int size = 10;
int arr[size];
}
Run Code Online (Sandbox Code Playgroud)
C++标准的8.3.4/1表示数组的大小必须是一个整数常量表达式,这size似乎不是.这是两个编译器中的错误,还是我错过了什么?
最新的VC++ CTP用这个有趣的消息拒绝代码:
error C2466: cannot allocate an array of constant size 0
Run Code Online (Sandbox Code Playgroud)
有趣的是,它似乎认为size是零.但至少它拒绝了代码.gcc和Clang应该不一样吗?
c++ arrays compile-time-constant variable-length-array c++11
有人曾暗示不建议在头文件中执行此操作:
using namespace std;
Run Code Online (Sandbox Code Playgroud)
为什么不建议?
它是否会导致像这样的链接器错误:(为了方便而行换行)
error LNK2005: "public: __thiscall std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >::
~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ)
already defined in tools.lib(Exception.obj)
Run Code Online (Sandbox Code Playgroud)