假设我有两个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.为什么?
根据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++ 11§27.5.4.2/ 21:
void swap(basic_ios& rhs);效果:
*this和rhs将被交换的状态,除了它rdbuf()应返回与函数调用之前rhs.rdbuf()返回的值相同的值,并且应返回与函数调用之前返回的值相同的值.
这部分交换对什么有用?
它会引起麻烦吗?
为什么这个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) 注意:这是一个问题的答案,以便记录其他人可能觉得有用的技术,以便可能意识到其他人更好的解决方案.请随意添加批评或问题作为评论.也可以随意添加其他答案.:)
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) 请考虑以下代码:
#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.
现在,我有另一个问题.我找不到任何文档说明这是标准行为.难道是保证这样的工作方式,始终?
码:
#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 … 注意:这是一个问题的答案,以便记录其他人可能觉得有用的技术,以便可能意识到其他人更好的解决方案.请随意添加批评或问题作为评论.也可以随意添加其他答案.:)
如何通过键入单个Windows命令来显示消息框,例如在"开始"菜单的"运行"对话框中,或者在[cmd.exe]命令解释程序中?
码:
#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++ ×9
c++11 ×7
unique-ptr ×2
c++14 ×1
cmd ×1
command ×1
constexpr ×1
constructor ×1
inheritance ×1
messagebox ×1
overriding ×1
shared-ptr ×1
standards ×1
swap ×1
templates ×1
visual-c++ ×1
weak-ptr ×1
windows ×1