小编Che*_*Alf的帖子

在构造函数中调用虚函数

假设我有两个C++类:

class A
{
public:
  A() { fn(); }

  virtual void fn() { _n = 1; }
  int getn() { return _n; }

protected:
  int _n;
};

class B : public A
{
public:
  B() : A() {}

  virtual void fn() { _n = 2; }
};
Run Code Online (Sandbox Code Playgroud)

如果我写下面的代码:

int main()
{
  B b;
  int n = b.getn();
}
Run Code Online (Sandbox Code Playgroud)

人们可能期望将n其设置为2.

事实证明,n设置为1.为什么?

c++ constructor overriding virtual-functions

220
推荐指数
6
解决办法
9万
查看次数

那么,std :: unique_ptr的自定义删除工作如何工作?

根据N3290 std::unique_ptr在其构造函数中接受一个删除参数.

但是,我不能在Windows中使用Visual C++ 10.0或MinGW g ++ 4.4.1,也不能在Ubuntu中使用g ++ 4.6.1.

因此,我担心我对它的理解是不完整或错误的,我不能看到显然被忽略的删除论证的观点,那么任何人都可以提供一个有效的例子吗?

我最好还想看看它是如何工作的unique_ptr<Base> p = unique_ptr<Derived>( new Derived ).

可能会使用标准中的一些措辞来备份示例,即使用您正在使用的任何编译器,它实际上做了它应该做的事情?

c++ unique-ptr delete-operator c++11

55
推荐指数
4
解决办法
4万
查看次数

为什么`basic_ios :: swap`只进行部分交换?

C++ 11§27.5.4.2/ 21:

void swap(basic_ios& rhs);

效果:*thisrhs将被交换的状态,除了它rdbuf()应返回与函数调用之前rhs.rdbuf()返回的值相同的值,并且应返回与函数调用之前返回的值相同的值.

这部分交换对什么有用?

它会引起麻烦吗?

c++ swap c++11

18
推荐指数
1
解决办法
453
查看次数

为什么这个constexpr静态成员函数在被调用时不被视为constexpr?

为什么这个constexpr static//! Nah注释标识的成员函数constexpr在调用时看不到?

struct Item_id
{
    enum Enum
    {
        size, position, attributes, window_rect, max_window_size, _
    };

    static constexpr int n_items_ = _;                          // OK
    constexpr auto member_n_items() const -> int { return _; }  // OK
    static constexpr auto static_n_items() -> int { return _; } // OK
    static constexpr int so_far = n_items_;                     // OK
    #ifndef OUT_OF_CLASS
        static constexpr int bah = static_n_items();            //! Nah.
    #endif
};

constexpr auto n_ids() -> int { …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr

17
推荐指数
2
解决办法
1904
查看次数

使用`std :: enable_if`时,如何避免编写`:: value`和`:: type`?[CPPX]

注意:这是一个问题的答案,以便记录其他人可能觉得有用的技术,以便可能意识到其他人更好的解决方案.请随意添加批评或问题作为评论.也可以随意添加其他答案.:)


在我的一些代码中,即标题rfc/cppx/text/String.h,我发现了以下神秘片段:

template< class S, class enable = CPPX_IF_( Is_a_< String, S > ) >
void operator!  ( S const& )
{ string_detail::Meaningless::operation(); }
Run Code Online (Sandbox Code Playgroud)

operator!支持String一个隐式转换为原始指针的类.所以我operator!为这个类和派生类重载(以及其他),因此无意中使用不受支持的运算符会给出一个合适的编译错误,提到它没有意义且无法访问.我认为这种使用方式比使用意外的错误结果而被默默接受更为可取.

CPPX_IF_宏支持Visual C++ 12.0(2013)及更早版本,它发现C++ 11 using大部分都超出了它的范围.对于更符合标准的编译器,我只会写...

template< class S, class enable = If_< Is_a_< String, S > > >
void operator!  ( S const& )
{ string_detail::Meaningless::operation(); }
Run Code Online (Sandbox Code Playgroud)

看起来 std::enable_if,

template< class S, class enabled = typename std::enable_if< Is_a_< String, S …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

16
推荐指数
3
解决办法
2165
查看次数

对象dtor中的`weak_ptr :: expired`行为

请考虑以下代码:

#include <iostream>
#include <memory>
using namespace std;

class T;

std::weak_ptr<T> wptr;

class T
{
public:
    T() {  }
    ~T() {
        std::cout << "in dtor" << std::endl;
        std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl;
    }
};

int main() {
    {
        auto ptr = std::make_shared<T>();
        wptr = ptr;
        std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我试图找出weak_ptrs在对象销毁阶段是否过期.看来是这样.输出是:

not expired
in dtor
expired
Run Code Online (Sandbox Code Playgroud)

我用gcc-5.1和ideone.

现在,我有另一个问题.我找不到任何文档说明这是标准行为.难道是保证这样的工作方式,始终

c++ shared-ptr weak-ptr c++11 c++14

16
推荐指数
3
解决办法
1029
查看次数

std :: reference_wrapper中的Visual C++ 10.0错误?

码:

#include <functional>

struct Foo
{
    virtual void mf() = 0;
};

struct Bar: Foo
{
    virtual void mf() {}
};

int main()
{
    Bar o;
    std::reference_wrapper<Foo const> wrapper( o );
}
Run Code Online (Sandbox Code Playgroud)

MinGW g ++ 4.6.1的结果:

[d:\dev\test]
> g++ foo.cpp -std=c++0x

[d:\dev\test]
> _

Visual C++ 10.0的结果:

[d:\dev\test]
> cl foo.cpp
foo.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class
        due to following members:
        'void Foo::mf(void)' : is abstract
        foo.cpp(5) : see declaration of 'Foo::mf'
        C:\Program …

c++ pass-by-reference c++-standard-library visual-c++ c++11

14
推荐指数
1
解决办法
1479
查看次数

Windows [cmd.exe]命令显示带超时的消息框?

注意:这是一个问题的答案,以便记录其他人可能觉得有用的技术,以便可能意识到其他人更好的解决方案.请随意添加批评或问题作为评论.也可以随意添加其他答案.:)


如何通过键入单个Windows命令来显示消息框,例如在"开始"菜单的"运行"对话框中,或者在[cmd.exe]命令解释程序中?

windows command cmd messagebox windows-shell

14
推荐指数
2
解决办法
6万
查看次数

虚拟继承中的优势

什么是C++ 98/C++ 03标准'和C++ 0x未来标准的虚拟继承优势的确切规则?

我不是只要求具体的段落,虽然我也要求它(在第10节的某个地方,我猜).

我还要问,标准清楚地说明了标准的后果.

c++ inheritance virtual-inheritance c++11

13
推荐指数
1
解决办法
1126
查看次数

`unique_ptr <T const []>`应该接受`T*`构造函数参数?

码:

#include <memory>
using namespace std;

struct T {};

T* foo() { return new T; }
T const* bar() { return foo(); }

int main()
{
    unique_ptr< T const >       p1( bar() );        // OK
    unique_ptr< T const [] >    a1( bar() );        // OK

    unique_ptr< T const >       p2( foo() );        // OK
    unique_ptr< T const [] >    a2( foo() );        // ? this is line #15
}
Run Code Online (Sandbox Code Playgroud)

Visual C++ 10.0和MinGW g ++ 4.4.1的示例错误:

[d:\dev\test]
> cl foo.cpp
foo.cpp …

c++ standards unique-ptr c++11

13
推荐指数
1
解决办法
799
查看次数