本文介绍了在std名称空间中定义的友善类:有保证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我回答这个问题:标准是否允许并保证有关friend标准库类和/或功能?

This question came up as I answered this question: does the standard allow and make any guarantees about friend-ing standard library classes and/or functions?

在这种情况下,问题是:

In this particular case, the situation the question was whether:

class MyUserDefinedType
{
    friend struct std::default_delete<MyUserDefinedType>;

private:
    ~MyUserDefinedType() { }
}

保证

允许使用默认删除器将MyUserDefinedType存储在std::unique_ptr<MyUserDefinedType>std::shared_ptr<MyUserDefinedType>对象中.

is guaranteed to allow MyUserDefinedType to be stored in a std::unique_ptr<MyUserDefinedType> or std::shared_ptr<MyUserDefinedType> object with the default deleter.

通常,标准库中描述的类是否需要直接实现其功能,还是可以使用任意级别的间接寻址?例如,

In general, are classes described in the standard library required to implement their functionality directly, or can they use any arbitrary level of indirection? For example, is it possible that

还是其他类似的东西?

我的猜测是不能保证能正常工作,但是我很好奇标准是否专门解决了这个问题.

My guess is that this is not guaranteed to work but I am curious if this is addressed specifically by the standard.

上面给出的这个特定示例适用于clang trunk(w/libc ++)和GCC 4.7.2(w/libstdc ++),FWIW

This specific example given above works for clang trunk (w/libc++) and GCC 4.7.2 (w/libstdc++), FWIW

推荐答案

否.根据C ++ 11标准第20.7.1.1.2段:

No. Per Paragraph 20.7.1.1.2 of the C++11 Standard:

namespace std {
    template <class T> struct default_delete {
        constexpr default_delete() noexcept = default;
        template <class U> default_delete(const default_delete<U>&) noexcept;
        void operator()(T*) const;
    };
}

它必须是类模板的事实已明确指定.这意味着它不能是别名模板.如果是这样,就不可能对其进行专门化处理.

The fact that it has to be a class template is explicitly specified. This means it cannot be an alias template. If that was the case, it would also be impossible to specialize it.

是.标准"中没有任何内容指定该呼叫不能由某些内部帮手完成.根据第20.1.1.2段:

Yes. Nothing in the Standard specifies that the call cannot be done by some internal helper. Per Paragraph 20.1.1.2:

void operator()(T *ptr) const;

4备注:如果T是不完整的类型,则程序格式错误.

4 Remarks: If T is an incomplete type, the program is ill-formed.

这仅指定在default_delete<>函子上调用call运算符的效果应该是什么,而不是如何具体实现(无论是否直接在体内)或通过将任务委托给其他类的某个成员函数来实现).

This only specifies what the effect of invoking the call operator on the default_delete<> functor should be, not how this shall be achieved concretely (whether directly inside the body of the call operator, or by delegating the task to some member function of some other class).

这篇关于在std名称空间中定义的友善类:有保证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!