在 C++ 中的文件 IO 期间捕获所有错误的最佳做法是什么?更具体地说,处理 ios 对象可能出现的错误的最佳实践是什么?例如,以下程序从磁盘读取文件并打印:
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <stdexcept>
// Defines a custom exception
struct MyException : public std::exception {
std::string s;
MyException(std::string s_) : s(s_) {};
const char * what () const throw () {
return ("MyException: " + s).c_str();
}
};
// Prints out nested exceptions
void print_exception(std::exception const & e, size_t const level = 0) {
std::cerr << std::string(level, ' ') << "exception: " << e.what() << …Run Code Online (Sandbox Code Playgroud) Haskell中记录类型和子类型的当前状态是什么?
我知道有一个关于事情做过类似超载记录名称等具体工作了,我想提出三个不同的记录类型A,B以及C在那里B并C包含所有相同的字段标签作为A,但不分享彼此的字段标签.然后,我希望能写的功能,其中f : A -> int,g: B -> int,h: C -> int其中功能f还接受带有类型的参数B和C.基本上,我想要B和C成为亚型A.更具体地说,如果我不必重复所有的字段标签,那就太好了.在伪代码中,这就像
data A = A { a :: String }
data B = B { A, b :: Char }
data C = C { C, c :: Float }
f :: A/B/C -> int
g :: B -> …Run Code Online (Sandbox Code Playgroud) 是否有一种简洁的方法可以将几乎所有构建标志传递给 CMake 中的外部项目?本质上,我有一个使用 ExternalProject_Add 的项目来下载和编译外部 CMake 项目。在一个特定平台上,我需要将大多数构建标志(如 CMAKE_OBJCOPY、CMAKE_OBJDUMP、CMAKE_RANLIB 等)修改为系统默认值以外的其他内容。现在,当我为调用 ExternalProject_Add 的父项目执行此操作时,我还想将这些选项传递给外部项目。当然,我可以通过在 ExternalProject_Add 中设置 CMAKE_ARGS 选项来做到这一点,但是有很多标志要复制。真的,我正在寻找一个复制所有标志然后允许我覆盖一些标志的选项。最后一点,因为我使用 ExternalProject_Add 下载档案,
C++ 11程序
struct Foo {
Foo(Foo const &) = delete;
};
int main() {
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
生成错误
$ g++ -std=c++11 junk.cpp -o junk
junk.cpp: In function 'int main()':
junk.cpp:6:9: error: no matching function for call to 'Foo::Foo()'
junk.cpp:6:9: note: candidate is:
junk.cpp:2:5: note: Foo::Foo(const Foo&) <deleted>
junk.cpp:2:5: note: candidate expects 1 argument, 0 provided
Run Code Online (Sandbox Code Playgroud)
现在,由于复制构造函数已被删除,因此无法生成默认构造函数.我将假设这是预期的行为,但在C++标准中它指定在删除复制构造函数时不应生成默认构造函数?
可以用运算符std::cout设置badbit或。如果是这样,这种情况何时发生?failbit<<
我们如何编写在C++中返回多个返回值的函数?更具体地说,如果一个函数返回一个元组,我们可以将这个函数与另一个没有明确接受元组的函数组合在一起吗?例如,在代码中:
#include <tuple>
#include <iostream>
std::tuple <int,int> tuple_ints(int x,int y) {
return std::tuple <int,int> (x,y);
}
int add(int x,int y) {
return x+y;
}
int main() {
std::cout << add(tuple_ints(1,2)) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我试图撰写的功能add和tuple_ints.这正确地产生了错误:
g++ -std=c++11 test01.cpp -o test01
test01.cpp: In function 'int main()':
test01.cpp:17:37: error: cannot convert 'std::tuple<int, int>' to 'int' for argument '1' to 'int add(int, int)'
std::cout << add(tuple_ints(1,2)) << std::endl;
^
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1 …Run Code Online (Sandbox Code Playgroud) 有没有一种很好的方法可以在没有调试符号的情况下获取OCaml中的异常行号?当然,如果我们打开调试符号并运行OCAMLRUNPARAM=b,我们就可以得到回溯.但是,我并不需要整个回溯,我想要一个没有调试符号的解决方案.目前,我们可以编写类似的代码
try
assert false
with x ->
failwith (Printexc.to_string x ^ "\nMore useful message")
Run Code Online (Sandbox Code Playgroud)
为了从断言中获取文件和行号,但这看起来很尴尬.有没有更好的方法来获取异常的文件和行号?
如何在 Rust 中在包含可迭代项的结构上定义迭代器?这是迭代器的一次尝试
use rand;
// Structure of items
struct Foo {
foo: Vec<f64>,
bar: Vec<i64>,
}
// Iterator for the structure
struct FooIter {
foo: Iterator,
bar: Iterator,
}
// Method that provides the iterator for use
impl Foo {
fn iter(&self) -> FooIter {
FooIter {
foo: self.foo.iter().peek(),
bar: self.bar.iter().peek(),
}
}
}
// Item desired from iterator
enum Bar {
MyFloat(f64),
MyInt(i64),
}
// Implementation of the iterator
impl Iterator for FooIter {
type Item = Bar; …Run Code Online (Sandbox Code Playgroud) 在Rust中使用Rc时,有什么好方法可以防止堆栈从递归释放中溢出吗?就我而言,我有一种可以增长到很大的图。当图表的一部分不再使用时,我希望它会被释放。但是,这会创建一系列的释放,最终使堆栈溢出。作为示例,请考虑以下代码:
// This is a test to see how long of a DAG that we can make before we have a
// problem freeing the graph due to a stack overflow from the memory frees.
// External libraries
use std::rc::Rc;
// Graph that contains integers
struct MyGraph {
// Random data
data : u32,
// Previous link
previous : Option <Rc <MyGraph>>,
}
// Show that we're freeing
impl Drop for MyGraph {
fn drop(&mut self) {
println!("{}",self.data);
}
} …Run Code Online (Sandbox Code Playgroud) C++中缩写函数的状态是什么?搜索一下,我在C++概念的工作草案中看到了一些提及.与此同时,GCC似乎没有像代码那样的问题
#include <iostream>
auto foo(auto x) {
std::cout << x << std::endl;
}
int main() {
foo(1);
foo(1.2);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我编译-Wpedantic,我收到警告:
g++ -std=c++14 -Wpedantic test08.cpp -o test08
test08.cpp:3:9: warning: ISO C++ forbids use of 'auto' in parameter declaration [-Wpedantic]
auto foo(auto x) {
^
Run Code Online (Sandbox Code Playgroud)
这告诉我缩写函数不完全符合标准.因此,它们在C++标准和常见C++编译器方面的当前状态是什么?