相关疑难解决方法(0)

可以在catch语句中复制省略吗?

考虑一个带有副作用的复制构造函数的异常类.

编译器是否可以跳过调用复制构造函数:

try {
    throw ugly_exception();
}
catch(ugly_exception) // ignoring the exception, so I'm not naming it
{ }
Run Code Online (Sandbox Code Playgroud)

那这个呢:

try {
    something_that_throws_ugly_exception();
}
catch(ugly_exception) // ignoring the exception, so I'm not naming it
{ }
Run Code Online (Sandbox Code Playgroud)

(是的,我知道这一切都非常难看,这是受到另一个问题的启发)

c++ optimization exception copy-elision

8
推荐指数
1
解决办法
258
查看次数

使用复制构造函数并且存在虚拟函数时,错误“在所有控制路径上递归”

下面的错误使我感到困惑。这是更复杂的代码的一小段。对我来说似乎很奇怪,只有同时存在模板化构造函数和虚拟方法才导致错误,并且仅在复制初始化对象时才引起错误。

有没有人有一个想法?谢谢。

    class A
    {
      long *p;
    public:
      A():p(0)
      {
      }

      template<class T>
      A(T val):p(val)// 1
      {
      }

      operator long*()
      {
       return p;
      }
    };

    class B
    {
      virtual void f()// 2
      {
      }
    };

    class C : public A, public B
    {
    };

    void main()
    {
      C c;
Run Code Online (Sandbox Code Playgroud)

下一行main()

      A a=c; 
Run Code Online (Sandbox Code Playgroud)

如果这两个行都标记为// 1和,则将触发以下错误// 2

warning C4717: 'C::C' : recursive on all control paths, function will cause runtime stack overflow 
Run Code Online (Sandbox Code Playgroud)

但是,在中使用以下代码时main(),不会出现错误:

      A …
Run Code Online (Sandbox Code Playgroud)

c++ templates virtual-functions multiple-inheritance visual-c++

5
推荐指数
1
解决办法
2513
查看次数