我想检查并将特定语句转换为try-with-ressources表单.但我觉得声明性提示格式的语法使我无法解决这个问题.
我试过了:
<!description="Stmt into try-with-resources">
try {
$before$;
someMethod($arg1, $arg2, $arg3);
$after$;
} catch $catches$
=>
try (Resource res = acquire($arg1, $arg2, $arg3)) {
$before$;
res.use();
$after$;
} catch $catches$
Run Code Online (Sandbox Code Playgroud)
但应用于我的代码,模式永远不会匹配.这是我希望匹配的一些示例代码部分:
public boolean step(String input) {
String id = getId(ID);
try {
SomethingBefore();
someMethod(10, "label", name);
return true;
} catch (Exception ex) {
log.error("problem", ex);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
知道为什么这不匹配吗?ESP.因为我认为文档中的示例与我的匹配,除了finally:
try {
$statements$;
} catch $catches$
finally {
$finally$;
}
Run Code Online (Sandbox Code Playgroud)
更新:看起来Jackpot -Rules使用相同的语法,可能是因为它使用相同的代码库.
我知道,通过多重继承,允许指针的值改变.但这也是单继承的情况吗?或者POD类型是什么?
你可能知道经典的例子:
#include <iostream>
using std::cout;
struct Base1 { virtual void f1() {} };
struct Base2 { virtual void f2() {} };
struct Derived : public Base1, public Base2 { virtual void g() {} };
int main() {
Derived d{};
auto *pd = &d;
auto pb1 = (Base1*)pd;
auto pb2 = (Base2*)pd;
cout << pd << "\n"; // say, 0x1000
cout << pb1 << "\n"; // say, 0x1000
cout << pb2 << "\n"; // say, 0x1008 !!!
}
Run Code Online (Sandbox Code Playgroud)
到目前为止这么好,这是很好的旧编译器实践.对象的布局方式是"根" Base2 …
override如果没有真正覆盖虚函数N3206,那么新的语法允许让编译器报告错误.
class Base {
virtual void vfunc();
void afunc();
};
Run Code Online (Sandbox Code Playgroud)
如class Derived : public Base标准示例中所述,以下情况将是错误:
void vfunk() override; // err: typovoid vfunc(int) override; // err: argumentvoid vfunc() const override; // err: cv 但是如果基本方法不是虚拟的呢?
void afunk() override; // ?void afunc(int) override; // ?void afunc() const override // ?;在N3291 "7.6.1.(3/5)属性语法和语义[decl.attr.grammar]"关于如何在源代码中编写属性我读了
有条件地支持使用属性范围的令牌,具有实现定义的行为.
和
对于未在本国际标准中指定的属性令牌,行为是实现定义的.
这是否意味着,属性规范如
[[ dllexport ]]
[[ hiding ]]
[[ unused ]]
[[ vendor::attrib ]]
Run Code Online (Sandbox Code Playgroud)
可能会被符合标准的编译器抱怨?那么,它可以报告错误并停止编译?
我希望编译器应该忽略它不知道如何处理它们的属性.好吧,这可能是危险的,因为属性拼写错误可能被忽视,例如[[ noretrun ]]或[[ carrys_dependencie ]]:-)
但特别是名称空间在这里会有所帮助,对吧?当我[[ gcc::mips ]]在Microsoft编译器上编译时,应该能够忽略它而不是拒绝它,对吧?
我读过N3291"12.8.(11/15/28)复制和移动类对象class.copy]"纠正隐式声明的移动构造函数
T(T&&)同样,移动分配,使用各自T operator=(T&&)的元素.
例:
struct CopyOnly {
CopyOnly();
CopyOnly(const CopyOnly&);
}; // declaring a copy means no implicit move.
struct Question {
std::vector<int> data_;
CopyOnly copyOnly_;
};
Run Code Online (Sandbox Code Playgroud)
班级 Question
=delete剐,因为非静态数据成员data_是只可复制,但不移动?更新.可以侧面的问题:对于Question q;将std::move(q)仍然有效?复制的后备是否会在那里发生?或者隐式声明的 move-ctor会强制编译器因错误而停止吗?在这里它编译.
更新2.如果我声明move-ctor,编译器为不可移动的数据成员生成什么Question(Question&&) =default?是否然后退回到复制那些?
我知道terminate()在线程变量离开作用域时调用的线程:
size_t fibrec(size_t n) {
return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}
int main() {
std::thread th{ fibrec, 35 };
// no join here
} // ~th will call terminate().
Run Code Online (Sandbox Code Playgroud)
th析构函数terminate()在离开作用域时会调用.
但那是什么future?他们运行的线程在哪里?它是分离的吗?它是如何结束的?
#include <iostream>
#include <future> // async
using namespace std;
size_t fibrec(size_t n) {
return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}
struct Fibrec {
size_t operator()(size_t n) { return fibrec(n); }
const size_t name_;
Fibrec(size_t name) : name_(name) {}
~Fibrec() { cerr …Run Code Online (Sandbox Code Playgroud) 允许Java注释的语义将它们放在函数体内的某个位置,例如注释特定的函数调用,语句或表达式?
例如:
class MyClass {
void theFunc(Thing thing) {
String s = null;
@Catching(NullPointerException) //<< annotation ?
s = thing.getProp().getSub().getElem().getItem();
if(s==null)
System.out.println("null, you fool");
}
}
Run Code Online (Sandbox Code Playgroud)
缩写经常写的(经常,绝对!):
class MyClass {
void theFunc(Thing thing) {
String s = null;
try {
s = thing.getProp().getSub().getElem().getItem();
} catch(NullPointerException ex) { }
if(s==null)
System.out.println("null, you fool");
}
}
Run Code Online (Sandbox Code Playgroud)
如果这个嵌入式注释的概念可行呢?或类似的东西?
我是约束编程的初学者,我在我的 c# 程序中使用Google or-tools 库。
我想向我的求解器添加以下约束:
((t1 >= 12 && t1 <= 15) || (t2 >= 16 && t2 <= 18)) && ( t1 + t2 ) < 30
所以我用c#写了下面一段代码:
var solver = new Solver("My_CP_Colver");
var t1 = solver.MakeIntVar(12, 20,"t1");
var t2 = solver.MakeIntVar(12, 20,"t2");
solver.Add(???)//<-((t1 >= 12 && t1 <= 15)||(t2 >= 16 && t2 <= 18)) && ( t1 + t2 ) < 30
Run Code Online (Sandbox Code Playgroud)
请帮助做出上述限制?
在阅读关于C++ 11时,我感觉当使用标准容器(如std::vector)和用户定义的数据类型时,鼓励人们提供noexcept移动操作,如果有的话,因为那时候容器会在内部真正移动数据而不是复制.
今天尝试时,我发现-std=c++1y(对于C++ 14)和g ++ - 4.8都没有区别.也许我错过了规范中的更新,也许我的例子是错误的.
我比较了三个应该可以移动的数据结构
noexceptnoexcept框架:
#include <string>
#include <vector>
#include <chrono>
#include <iostream> // cout
using std::vector; using std::cout;
using namespace std::chrono;
long long millisSeit(steady_clock::time_point start) {
return duration_cast<milliseconds>(steady_clock::now()-start).count();
}
namespace {
constexpr size_t ITERATIONS = 1000*1000;
template<typename ELEM>
void timeStuff(std::string name) {
cout << name << "...";
auto start = steady_clock::now();
std::vector<ELEM> data{};
for(size_t idx=0; idx<ITERATIONS; ++idx) {
data.emplace_back( idx …Run Code Online (Sandbox Code Playgroud) 是否允许增加it已经存在的迭代器变量end(),即auto it = v.end()?
vector?++it可能幂等如果it==v.end()?我问,因为我偶然发现了这样的代码:
std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7 };
// delete every other element
for(auto it=v.begin(); it<v.end(); ++it) { // it<end ok? ++it ok on end?
it = v.erase(it);
}
Run Code Online (Sandbox Code Playgroud)
它适用于g ++ - 6,但这不是证据.
对于一个人it<v.end()可能只能使用vectors,我想它应该it!=v.end()一般阅读.但是在这个示例中,当它已经结束时,将无法识别vif ++it的结尾.