小编atk*_*ins的帖子

在S s = S()中,是否保证不会创建临时值?

在下面的代码中,pSs.pS保证在最终的线相等?换句话说,在声明中S s = S();,我可以确定S不会构建临时的吗?

#include <iostream>
using namespace std;

struct S
{
  S() { pS = this; }
  S* pS;
};

int main()
{
  S s = S();
  S* pS = &s;
  cout << pS << " " << s.pS << endl;
}
Run Code Online (Sandbox Code Playgroud)

在每个编译器中我都测试了这个pS == s.pS,但是我对标准不够熟悉,以至于能够确保这是有保证的.

c++ constructor temporary language-lawyer

14
推荐指数
3
解决办法
879
查看次数

如何在Visual Studio 2010中禁用返回值优化?

是否可以在Visual Studio 2010中禁用RVO(返回值优化)?将优化标志设置为/Od(关闭所有优化)并没有帮助.在g ++中,存在-fno-elide-constructors禁用RVO的标志.

c++ visual-studio-2010 visual-studio return-value-optimization

9
推荐指数
1
解决办法
4061
查看次数

模板实例化的范围解析

我有以下一组类(我的实际情况的最小复制):

namespace Parent
{
  class A {};

  namespace Nested
  {
    class A {};
  }

  template <typename T>
  class B
  {
    A myA;
  };
}
Run Code Online (Sandbox Code Playgroud)

我希望该成员Parent::B::myA应该毫不含糊地解决为类型Parent::A.但是,在我的项目的其他地方我有这个:

namespace Parent
{
  using namespace Nested;

  void foo()
  {
    B<int> myB;
  }
}
Run Code Online (Sandbox Code Playgroud)

无法在MSVC 2003下编译:

error C2872: 'A' : ambiguous symbol
        could be 'Test.cpp(5) : Parent::A'
        or       'Test.cpp(9) : Parent::Nested::A'
        Test.cpp(26) : see reference to class template instantiation 'Parent::B<T>' being compiled
        with [ T=int ]
Run Code Online (Sandbox Code Playgroud)

如果我明确声明B::myA,即代码将编译Parent::A myA;.但是,代码在 …

c++ templates visual-c++ name-lookup

6
推荐指数
1
解决办法
395
查看次数

如何使用VS Debugger找出模板参数的类型?

我正在处理大量的模板化代码,现在需要弄清模板参数的类型。

在我简化的以下代码中,如何调试以找出每个T依赖于main()的类型,它将根据int,double或任何其他类型来初始化A。

template <class T>
class A // 1
{
public:
    typedef T Type;
};

template <class T>
class A<T*> // 2
{
public:
    typedef T Type;
};

template <class T> 
class A<T**> // 3
{
pbulic:
    typedef T Type;
};
Run Code Online (Sandbox Code Playgroud)

我尝试使用“ 监视”窗口,但我认为它不能帮助我了解T的实际类型。

如果T是int *,那么最好以模板化代码的实例化形式查看代码,就像这样;

 class A // 1
 {
  public:
    typedef int* Type;
 };
Run Code Online (Sandbox Code Playgroud)

提前致谢。

c++ templates visual-studio

3
推荐指数
1
解决办法
331
查看次数

用不同的参数复制c ++中的构造函数

为什么这个代码在传递的对象不是Line类型时调用复制构造函数,并且没有等于operator/explicit调用.A行和A行()之间有区别吗?
我从许多在线教程中读到它应该是Line类型.我是C++的新手.请帮忙

 #include <iostream>
    using namespace std;

class Line
{
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    // allocate memory for the pointer;
    ptr = new int;
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << endl;
    ptr …
Run Code Online (Sandbox Code Playgroud)

c++ pass-by-reference copy-constructor

2
推荐指数
1
解决办法
155
查看次数

当 x != 1 和 y != 0 时,x * y == y 可以吗?

如果xy是 IEEE 754 浮点数(单精度或双精度),我们可以有

x * y == y
Run Code Online (Sandbox Code Playgroud)

其中x != 1and y != 0(and nory等于+inf, -inf, or nan)。

c# ieee-754

0
推荐指数
1
解决办法
99
查看次数