正如我在cppreference中看到的那样 ,经典的"throw"声明列表现在已在C++ 11中弃用.离开这个机制的原因是什么?我应该如何指定抛出我的函数的异常?
令我惊讶的是,typename当依赖类型作为基类出现时,没有必要添加:
struct B {};
struct wr
{ typedef B type; };
template<class T>
struct A : T::type
{};
int main()
{
A<wr> a;
(void)a;
}
Run Code Online (Sandbox Code Playgroud)
为什么typename不需要在前面T::type?
考虑这个函数:
std::vector<unsigned> copy(std::vector<unsigned> const& v) noexcept
{ return v; }
int main()
{
try {
(void)copy({1, 2, 3});
} catch(...) {}
}
Run Code Online (Sandbox Code Playgroud)
返回对象的副本构造可能会抛出。在这种情况下,异常是否会传播给调用者(即它被认为发生在main),从而在处理catch(...)程序中进行处理?或者异常会遇到noexcept并导致调用std::terminate()?
C++17/C++20 中关于生命周期规则的变化(标准化 RVO、临时物化、隐式对象创建等)是否相对于标准的先前版本改变了这方面的一些规则?
我有一个源目录,其中包含一个名为"phantom-dir /"的文件夹,其中我放置了我不需要的所有生成的文件.我想将CMake中的所有生成的文件放在这个幻像目录中(与其他生成的和"丑陋的"文件一起).
一个小例子:
$ mkdir cmake-test
$ cd cmake-test
$ echo 'message("Hello World!")' > CMakeLists.txt
$ cmake . | grep "Hello"
Hello World!
$ tree
.
??? CMakeCache.txt
??? CMakeFiles
? ??? CMakeCCompiler.cmake
? ??? cmake.check_cache
? ??? CMakeCXXCompiler.cmake
? ??? CMakeDetermineCompilerABI_C.bin
? ??? CMakeDetermineCompilerABI_CXX.bin
? ??? CMakeDirectoryInformation.cmake
? ??? CMakeOutput.log
? ??? CMakeSystem.cmake
? ??? CMakeTmp
? ??? CompilerIdC
? ? ??? a.out
? ? ??? CMakeCCompilerId.c
? ??? CompilerIdCXX
? ? ??? a.out
? ? ??? CMakeCXXCompilerId.cpp
? …Run Code Online (Sandbox Code Playgroud) 如果我将一个函数标记为noexcept(false),或任何其他评估为false的表达式,它意味着什么?(1)我是否向编译器确保该函数可以抛出异常?,(2)或者我是否确定它是否可以抛出异常?
最后,如果省略说明noexcept符,它等同于noexcept(false)或仅等同于上述的(2)nd含义?
是否有可能在HTML中编写如下内容:
<a href="bla bla bla bla\
bla bla bla bla">....</a>
Run Code Online (Sandbox Code Playgroud)
这个想法是在不同的行中分割字符串属性以提高可读性.
如果声明的函数具有noexceptspecificator( ,noexcept,noexcept(true),noexcept(false)或任何其他noexcept(expr)计算结果为true或false),但它在另一个地方定义的,做我需要指定noexcept再次定义符,或仅在其向前声明?
码:
// test3.cpp
#include <stack>
using namespace std;
template<typename T>
struct ptr_stack_tp;
template<typename T>
struct ptr_stack_tp<T*> : public stack<T*>
{
~ptr_stack_tp()
{
while (!empty()) {
operator delete(top());
pop();
}
}
};
int main()
{}
Run Code Online (Sandbox Code Playgroud)
错误消息(gcc 4.7.2):
test3.cpp: In destructor 'ptr_stack_tp<T*>::~ptr_stack_tp()':
test3.cpp:15:23: error: there are no arguments to 'empty' that depend on a template parameter, so a declaration of 'empty' must be available [-fpermissive]
test3.cpp:15:23: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an …Run Code Online (Sandbox Code Playgroud) #include <utility>
template<class T1, class T2>
struct mypair : std::pair<T1, T2>
{ using std::pair<T1, T2>::pair; };
int main()
{
(void)std::pair(2, 3); // It works
(void)mypair(2, 3); // It doesn't work
}
Run Code Online (Sandbox Code Playgroud)
以上是否形成良好?
如果继承构造函数,是否可能在第二种情况下推导出类模板参数?构建者是否std::pair参与创建隐式演绎指南mypair?
我的编译器是g ++ 7.2.0.
我想获取从当前文件夹到/. 例如,对于目录:/var/lib/program/subfolder,我想要一个输出,例如:
$ pwd
/var/lib/program/subfolder
$ magic_ls_-l_command somefile
drwxr-xr-x 10 root root 4096 May 15 20:20 var
drwxr-xr-x 10 root root 4096 May 15 20:20 lib
drwxrwxr-x 10 root user 4096 May 16 20:21 program
drwxrwxr-x 10 root user 4096 May 16 20:21 subfolder
-rwxrwxr-- 1 root user 4096 May 16 20:22 somefile
Run Code Online (Sandbox Code Playgroud)
我不在乎顺序(从/var到/subfolder或相反)、硬链接的数量甚至日期。我只是将它们写下来以模拟ls -l输出。另外,我不在乎每个文件名如何打印(/var和/lib、var和lib、或/var和/var/lib)。我只对所选文件的路径中每个文件/目录的所有权或pwd到/ …