有样品
// Example 2: Will this compile?
//
// In some library header:
namespace N { class C {}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
int main()
{
N::C a[10];
std::accumulate(a, a+10, 0);
}
来自“例外C++:47个工程难题,编程问题和解决方案”名称查找和接口原理-第4部分
g++
我对namespace.udirp2的含义有点困惑。考虑以下方案:
namespace X { int i = 1; }
namespace Y { using namespace X; }
int main() { i = 2; }
在它的名字-查找i的主要失败(我尝试了GCC,Clang和可视c++)。这似乎不符合namespace.udirp2 ():
使用-指令指定指定命名空间中的名称可以在使用-指令之后出现的作用域中使用。在非限定名称查找(basic.lookup.unqual)期间,这些名称似乎是在最近的封闭名称空间中声明的,其中包含using-指令和指定的命名空间。注意:在
我正在遵循C++入门书,并尝试所有的代码示例。我对这个很感兴趣:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string line;
while (getline(cin,line))
cout << line << endl;
return 0;
}
在编译此代码之前,我猜测编译将失败,因为我没有使用
wh
我不明白为什么我会犯这个“奇怪的”错误。我读了类似的问题,但它没有回答我的问题。如果我在main函数而不是全局作用域中定义数组,就不会出现错误。但是假设我必须在全局范围内定义这个数组。为什么我要接受这个错误?代码如下:
#include <iostream>
#include <cstring>
using namespace std;
int right[1005];
int main()
{
memset(right,0,sizeof(right));
return 0;
}
下面是错误:
memset2.cpp: In function ‘int ma
您能解释一下在该代码中名称空间查找失败的原因吗? namespace B {
namespace C {
int i;
}
}
namespace A {
namespace B {
void foo() {
// why does not much A::B::C
B::C::i = 3;
}
}
} 是的,我知道::B::C::我之所以工作,是因为我们指示全局名称空间,但我很好奇为什么当我们不使用::before B时,look-up不能在B::C名称空间之外搜索。 提前感谢
我已经习惯了Java/C#处理包的方式,尽管我在C++中做了大部分工作,但我已经有一段时间不使用命名空间了。
我知道我可以有这样的多个名称空间:
namespace A
{
class Foo;
}
namespace B
{
namespace C
{
class Bar;
}
}
这个很好。我所想要想的是下面的场景:
namespace A
{
class Foo;
}
namespace B
{
namespace A
{
class Bar : public A:Foo //here A sho
我有下面的代码,如果我包含了size_t,它定义的std::size_t和::size_t等同于<cstddef>。
// h.hpp
namespace N {
using size_t = decltype(sizeof(int));
}
// a.hpp
#include <h.hpp>
namespace N {
class C {
size_t size() const;
};
void f(size_t);
}
// ^^^ These use N::size_t!
std::size_t是以任何方式违反C++标准的,这是否会在任何同
这是出现在"C++ Primer“第13.3节中的有关代码,第5版:
void swap(Foo &lhs, Foo &rhs)
{
using std::swap;
swap(lhs.h, rhs.h); // uses the HasPtr version of swap
// swap other members of type Foo
}
书中提到了类特定的交换空间不被使用声明隐藏的现象,并请读者参阅第18.2.3节:
我阅读了这一节,并意识到这可能与依赖于参数的查找(ADL)有关。摘录如下:
但我在理解上还是有些模糊
我有以下C++代码:
//Define to 1 to make it work
#define WORKS 0
#if WORKS
template< typename T > struct Foo;
#else
template< typename T >
struct Foo {
T t;
};
#endif
class Bar; //Incomplete type
void fFooBar(Foo<Bar> const & foobar) { }
void f(Foo<Bar&g
考虑以下方案:
namespace foo {
namespace sub {
int f();
} // namespace sub
} // namespace foo
namespace bar {
namespace sub {
int g() {
using namespace foo;
return sub::f() + 1;
}
} // namespace sub
} // namespace bar
我希望这能编译,但是-它没有:
$ g++-6 -c a.cpp
a.cpp: In function ‘int bar::sub::g()’:
首先,请随意为这个问题建议更好的标题。
考虑下列方案:
#include <numeric>
namespace N { class C {}; }
int operator+( int i, N::C ) { return i+1; }
int main() {
N::C a[10];
std::accumulate( a, a+10, 0 );
}
g++ 5.4.0:成功编译(参见实时演示 )
clang++ 3.8.0 (参见实时演示 )
错误:
In file included from source_file.cpp:3:
/usr/include
考虑下列方案:
struct S {
using T = float;
operator T() { return 9.9f; }
};
int main() {
S m;
S::T t = m;
t = m.operator T(); // Is this correct ?
}
该程序在g++中编译得很好(参见实时演示 )
但在clang++、MSVC++和Intel C++编译器中编译失败。
clang++给出了以下错误(参见实时演示 )
main.cpp:8:20: error: unknown type name 'T'; di