以下代码在gcc 4.8.1上编译没有问题:
#include <utility>
struct foo
{
};
int main()
{
foo bar;
foo() = bar;
foo() = std::move( bar );
}
Run Code Online (Sandbox Code Playgroud)
似乎隐式生成的赋值运算符foo不是&ref-qualified,因此可以在rvalues上调用.根据标准,这是正确的吗?如果是这样,有什么理由不要求隐式生成的赋值运算符被重新&限定?
为什么标准不要求生成以下内容?
struct foo
{
foo & operator=( foo const & ) &;
foo & operator=( foo && ) &;
};
Run Code Online (Sandbox Code Playgroud) 假设我有一堆矩形,其中一些是相交的,有些是孤立的.E. g.
+--------------- + +-------- +
| | | |
| | | |
| A | | C |
| +---------------- + | |
| | | | +---------+-------- +
| | | | | |
+---------|----- + B | | D |
| | | |
| | +------------------ +
+---------------- +
+------------------ + +-------- +
| | | |
| E | | X |
+-------------------+ | |
| | +-------- +
| | +------------ +
| … 假设我有一种既不可移动也不可复制的类型:
struct foo
{
explicit foo( size_t ){}
~foo(){}
foo( foo const & ) = delete;
foo( foo && ) = delete;
foo& operator=( foo const & ) = delete;
foo& operator=( foo & ) = delete;
};
Run Code Online (Sandbox Code Playgroud)
现在给出一个在编译时已知的数字(称为N),有没有什么方法可以在堆栈上创建这些序列,每个都用数字0到N-1初始化?我会满足于C数组foo[N],一个std::array< foo, N >,或者甚至是std::tuple某种形式的.
我想避免的是写出来:
foo f0( 0 ), f1( 1 ), ... fNminus1( N-1 );
Run Code Online (Sandbox Code Playgroud)
当感觉这是编译器应该能够为我做的事情.我能想到的最好的就是使用boost::optional.
boost::optional< foo > f[N];
for( size_t i = 0U; i < N; ++i )
f[i] = boost::in_place( …Run Code Online (Sandbox Code Playgroud) 以下代码在使用 -std=c++11 的 GCC 6.3 中可以正常编译。
template <typename T>
struct MetaFunction
{
static int value;
};
#define MY_MACRO(T) (MetaFunction<T>::value)
template <class T, const int* p = &MY_MACRO(T)>
struct Foo
{
void DoSomething()
{
}
};
int main()
{
Foo< int > f;
f.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
但如果指定 -std=c++14 则会发出以下错误:
main.cpp:19:14: error: '& MetaFunction<int>::value' is not a valid template argument for 'const int*' because it is not the address of a variable
Foo< int > f;
^
Run Code Online (Sandbox Code Playgroud)
无论指定 -std=c++11 还是 -std=c++14,Clang 3.8 都会给出类似的错误。 …
我的任务是在矩阵中找到从一个点到另一个点的最短路径.可以仅在这样的方向上移动(向上,向下,向左,向右).
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 1 0 1 F 0
0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 0
0 S 0 1 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
Run Code Online (Sandbox Code Playgroud)
S - 起点
F - 目的地(完成)
0 - 免费细胞(我们可以穿过它们)
1 - "墙壁"(我们不能穿过它们)
很明显,广度优先搜索以最佳方式解决了这个问题.我知道Boost库提供了这个算法,但我之前没有Boost.
如何使用Boost在我的案例中进行广度优先搜索?据我所知,Boost的广度优先搜索算法仅适用于图形.我想将矩阵转换为带m*n …
c++ ×5
c++11 ×2
algorithm ×1
boost ×1
boost-graph ×1
c++14 ×1
clang ×1
compile-time ×1
constructor ×1
gcc ×1
matrix ×1
merge ×1
rvalue ×1