那我们会得到UB吗?我试过这个:
#include <iostream>
struct B
{
B(){ std::cout << "B()" << std::endl; }
~B(){ std::cout << "~B()" << std::endl; }
};
struct A
{
B b;
A(){ std::cout << "A()" << std::endl; throw std::exception(); }
~A(){ std::cout << "~A()" << std::endl; }
};
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
desctructor没有被要求netither A也没有B.实际输出:
B()
A()
terminate called after throwing an instance of 'std::exception'
what(): std::exception
bash: line 7: 21835 Aborted (core dumped) ./a.out
Run Code Online (Sandbox Code Playgroud)
http://coliru.stacked-crooked.com/a/9658b14c73253700
因此,在块范围变量初始化期间构造函数抛出的任何时候,我们都得到UB吗?
首先,我是Java编码器,想要了解c ++中的多态性.我为了学习目的编写了这个例子:
#include<iostream>
using namespace std;
class A
{
public:
virtual void foo(){ std::cout << "foo" << std::endl; }
};
class B : public A
{
public:
void foo(){ std::cout << "overriden foo" << std::endl; }
};
A c = B();
int main(){ c.foo(); } //prints foo, not overriden foo
Run Code Online (Sandbox Code Playgroud)
我预计overriden foo会打印,但事实并非如此.为什么?我们覆盖了该方法foo,class B并且我认为决定应该调用哪个方法是从对象的运行时类型开始,在我的例子中B,但不是静态类型(A在我的例子中).
现场的例子就在那里
来自Scott Meyers Effective C++:
如果你从不调用模拟非本地静态对象的函数,那么你永远不会产生构造和破坏对象的成本,这对于真正的非本地静态对象来说是不可能的.
功能:
FileSystem& tfs()
{
static FileSystem fs;
return fs;
}
Run Code Online (Sandbox Code Playgroud)
但标准说:
具有静态存储持续时间的块范围实体的常量初始化(3.6.2)(如果适用)在首次输入块之前执行.允许实现 在静态或线程 存储持续时间 内执行其他块范围变量的早期初始化,条件是允许 实现在命名空间范围内静态初始化具有静态或线程存储持续时间的变量(3.6.2).
这意味着fs即使我们不调用该函数,我们也无法确定该变量是否初始化tfs().因为允许实现对具有静态存储持续时间的变量执行早期初始化.
谁是对的或我错过了什么?
我写了以下代码:
struct A{
int a;
int b;
A(int c): a(c), b(a){ }
};
int main()
{
A b(10);
}
Run Code Online (Sandbox Code Playgroud)
现在,我不确定b用aas 初始化a(c), b(a).可以这样做还是可能导致UB?
在关闭ActorSystem的时候,我发现它ActorSystem::shutdown已被弃用了.这是建议的"Use the terminate() method instead".
但这些方法的解释几乎相同:
/**
* Terminates this actor system. This will stop the guardian actor, which in turn
* will recursively stop all its child actors, then the system guardian
* (below which the logging actors reside) and the execute all registered
* termination handlers (see [[ActorSystem#registerOnTermination]]).
* Be careful to not schedule any operations on completion of the returned future
* using the `dispatcher` of this actor system as it will have …Run Code Online (Sandbox Code Playgroud) 我是新手,CLion并尝试编写一个简单的程序来了解它是如何工作的.我参与Windows 8 + cygwin的计划是:
int main()
{
throw std::exception();
}
Run Code Online (Sandbox Code Playgroud)
我在控制台输入了输出:
C:\....\bin.exe
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
有关该计划的消息被中止或其他什么?什么都没有,我怎么能通过抛出异常来检测我的程序是否真的被中止?
我正在阅读 Scott Meyers C++,并遇到了所谓的代码膨胀的概念。他提供了一个如何通过继承来减少它的示例:
template <typename T>
class SquareMatrixBase{
protected:
void invert(std::size_t matrixSize); // <------------ HERE
}
template <typename T, std::size_t n>
class SquareMatrix : private SquareMatrixBase<T>{
private:
using SquareMatrix<T>::invert;
public:
void invert(){ invert(n) }
}
Run Code Online (Sandbox Code Playgroud)
现在,在该项目的摘要中,他指出
模板生成多个类和多个函数,因此任何不依赖于模板参数的模板代码都会导致膨胀。
因此,在我们的示例中SquareMatrixBase<T>::invert(std::size_t),它不依赖于模板参数。因此,它会导致代码膨胀。这不是我们试图消除的东西吗?我错过了什么?
我正在阅读M. Odersky的Scala编程,现在我正在努力理解运算符的含义.据我所知,Scala中的任何运算符都只是一种方法.请考虑以下示例:
class OperatorTest(var a : Int) {
def +(ot: OperatorTest): OperatorTest = {
val retVal = OperatorTest(0);
retVal.a = a + ot.a;
println("=")
return retVal;
}
}
object OperatorTest {
def apply(a: Int) = new OperatorTest(a);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们在这个类中只定义了+运算符.如果我们键入这样的东西:
var ot = OperatorTest(10);
var ot2 = OperatorTest(20);
ot += ot2;
println(ot.a);
Run Code Online (Sandbox Code Playgroud)
然后
=+
30
Run Code Online (Sandbox Code Playgroud)
将是输出.所以我假设对于Scala中的每个类(或类型?),我们都+=为它定义了运算符,如a += b iff a = a + b.但是因为每个运算符只是一个方法,其中+ =运算符定义了?也许有一些类(如Object在Java中)包含这些运算符的所有defenition等等.
我看着AnyRef希望找到,但不能.
我正在阅读M. Odersky的Scala编程,他说
像approximate这样自称为最后一个动作的函数叫做tail recursive.
所以,我试过这个:
object Main extends App {
implicit val mc = new MyClass(8)
val ti = new TestImplct
ti.test
}
class TestImplct {
def test(implicit mc : MyClass): Unit = {
println(mc.i)
mc.i -= 1
if(mc.i < 0){
throw new IllegalArgumentException
}
test
}
}
class MyClass(var i : Int)
Run Code Online (Sandbox Code Playgroud)
但它会生成以下堆栈跟踪
Exception in thread "main" java.lang.IllegalArgumentException
at TestImplct.test(Main.scala:13)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
at TestImplct.test(Main.scala:15)
Run Code Online (Sandbox Code Playgroud)
这意味着它为每个递归调用生成一个新的堆栈帧.但最后一步是呼唤自己.什么是错的,如何让它尾递归? …