【问题标题】:Can std::forward_list members be implemented as static?std::forward_list 成员可以实现为静态的吗?
【发布时间】:2011-12-10 21:10:11
【问题描述】:

std::forward_list 提供 insert_aftererase_after 成员,它们可能不需要实际访问 std::forward_list 对象。因此,它们可以实现为static 成员函数,并且可以在没有列表对象的情况下调用——这对于想要从列表中删除自身的对象很有用,这是一种非常常见的用途。 编辑:此优化仅适用于std::allocator 上的forward_list 特化或用户定义的无状态分配器。

符合标准的实现可以做到这一点吗?

§17.6.5.5/3 说

对 C++ 标准库中描述的成员函数签名的调用就像实现 声明没有额外的成员函数签名。

带脚注

一个有效的 C++ 程序总是调用预期的库成员函数,或具有等效行为的函数。实现还可以定义其他成员函数,否则有效的 C++ 程序不会调用这些函数。

我不清楚添加 static 是否会创建一个“不同的”成员函数,但删除(隐式)参数不应破坏添加默认参数不会破坏的任何内容,这是合法的。 (您不能合法地将 PTMF 带入任何标准成员函数。)

我觉得应该允许图书馆这样做,但我不确定是否会违反某些规则。列出的成员函数原型有多规范?

【问题讨论】:

  • 变异列表操作需要访问列表的分配器,所以我怀疑它们可能是静态的(尤其是新的有状态分配器)。
  • 不过,模板可以专门用于std::allocator 的极其常见的情况,如果需要,用户也可以自己使用。
  • 仅凭关于 iterator 的知识,您将如何专门处理此问题?迭代器不知道它属于哪个列表,也不知道该列表使用哪个分配器。
  • @KerrekSB 仅给定迭代器,您将需要使用特定于静态成员函数的调用语法,这将是非标准的。该模板将专门用于分配器是 std::allocator 的知识,它始终是无状态的。
  • 与这个话题相关的是Scott Meyers的一篇文章,“How Non-Member Functions Improve Encapsulation”,非常值得一读。

标签: c++ c++11 c++-standard-library


【解决方案1】:

标准说,如果没有人能分辨出区别,你就可以侥幸逃脱。而且你是正确的,不能合法地将 PTMF 创建到 forward_list,所以这样你是安全的。

已经指出了自定义分配器的危险。但即使对于 std::allocator<T>,也有可能有人专门化 std::allocator<MyType>,然后检测到 allocator::construct/destroy 没有被调用。

好的,但是可以专门说std::forward_list<int>(没有自定义分配器,没有用户定义的 value_type)并将insert_after 设为静态吗?

没有。这种变化可以通过新的 SFINAE 功能检测到。这是一个演示:

#include <memory>
#include <iostream>

template <class T, class A = std::allocator<T>>
class forward_list
{
public:
    typedef T value_type;
    struct const_iterator {};
    struct iterator {};

    iterator insert_after(const_iterator p, const T& x);
};

template <class C>
auto test(C& c, typename C::const_iterator p, const typename C::value_type& x)
    -> decltype(C::insert_after(p, x))
{
    std::cout << "static\n";
    return typename C::iterator();
}

template <class C>
auto test(C& c, typename C::const_iterator p, const typename C::value_type& x)
    -> decltype(c.insert_after(p, x))
{
    std::cout << "not static\n";
    return typename C::iterator();
}

int main()
{
    ::forward_list<int> c;
    test(c, ::forward_list<int>::const_iterator(), 0);
}

这个程序运行并打印出来:

not static

但如果我将insert_after设为静态:

static iterator insert_after(const_iterator p, const T& x);

然后我得到一个编译时错误:

test.cpp:34:5: error: call to 'test' is ambiguous
    test(c, ::forward_list<int>::const_iterator(), 0);
    ^~~~
test.cpp:16:6: note: candidate function [with C = forward_list<int, std::__1::allocator<int> >]
auto test(C& c, typename C::const_iterator p, const typename C::value_type& x)
     ^
test.cpp:24:6: note: candidate function [with C = forward_list<int, std::__1::allocator<int> >]
auto test(C& c, typename C::const_iterator p, const typename C::value_type& x)
     ^
1 error generated.

检测到差异。

因此将forward_list::insert_after设为静态是不符合标准的。

更新

如果您想使“静态”重载可调用,您只需使其比“非静态”重载更可取。一种方法是将“非静态”重载更改为:

template <class C, class ...Args>
auto test(C& c, typename C::const_iterator p, const typename C::value_type& x, Args...)
    -> decltype(c.insert_after(p, x))
{
    std::cout << "not static\n";
    return typename C::iterator();
}

现在测试将根据insert_after 成员函数是否为静态打印出“静态”或“非静态”。

【讨论】:

  • 没想到用户专用std::allocator,很好。但是"static" 不是重载了一个模板,没有可能的格式良好的专业化,因此格式不正确,不需要诊断吗?
  • 使用功能齐全的“静态检测器”更新答案。
  • 好吧...不难,但这与例如通过查找非重载成员函数的类型来检测默认参数有何不同?现在模板产生了一个有效的特化,但它似乎仍然是 UB,因为表达式C::insert_after() 没有被标准定义。同样,Allocator::construct 的可变参数模板实现在 C++03 中是否不兼容,因为它可以被称为construct&lt;&gt;(...)?关于库规范的深度,这确实是原始问题的要点。
  • 也许根本没有。除了 [member.functions]/p2 专门允许实现向非虚拟成员函数添加(或通过重载减去)默认参数。但是没有关于使成员函数静态化的权限。我们在 C++11 中的新 SFINAE 功能甚至对委员会成员来说都是新的,我毫不怀疑我们都将在未来 5 年内接受教育。毫无疑问,由于这种继续教育,语言和图书馆将继续发展。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 2011-12-18
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 2018-09-03
相关资源
最近更新 更多