是否有技术原因不允许使用声明来使用不同的名称访问函数名称?

Pix*_*ist 16 c++ language-lawyer

考虑

namespace foo
{
  namespace bar
  {
    void f();
    void f(int);
  }
}
Run Code Online (Sandbox Code Playgroud)

foo一个可以使所有foo::bar::f可访问为foo::fvia

using bar::f; // in foo
Run Code Online (Sandbox Code Playgroud)

是否有任何技术原因,语法的不存在性,使所有foo::bar::f可访问foo::g

using bar::f as g; 
// or in line with using declarations for types:
using g = bar::f;
Run Code Online (Sandbox Code Playgroud)

或者有这样的事情甚至被考虑但被拒绝了?(为什么?)

Col*_*mbo 23

N1489:

可以将类别和命名空间之外的别名概念概括为函数,变量等.我们没有看到这样做的充分好处,可以想象严重过度使用会导致混淆使用哪些函数和变量.因此,我们不建议本节中提到的概括.此外,我们不打算进一步研究这些概括,除非有人提出表明重要用途的例子.


Tar*_*ama 10

我想不出任何技术原因,我认为这是一个合适的(非冲突)语法和一个非常重要的用例的问题.

目前有一个标准提议是为语言添加函数别名以支持opaque typedef.根据那篇论文,这个特征以前在90年代初考虑过,但被拒绝了:

Stroustrup在他的D&E书中将[函数别名]描述为在解决由于多重继承引起的名称冲突的上下文中的"重命名"特性:"这个概念的语义很简单,实现很简单; 问题似乎是要找到一个合适的语法."他说这样的提议"是在1990年的西雅图标准会议上提出的",虽然最初有"绝大多数",但这个特征最终没有被采纳: "在下次会议上,...我们同意这种名称冲突不太可能足以保证单独的语言功能."

建议的语法如下:

template< class RA, class R = std::less<> >
void
sort( RA b, RA const e, R lt = {} )
{
    using operator<() = lt; // operator < has type R
    // Remaining code in this scope uses infix < in place of calls to lt().
    // (A future proposal may suggest synthesis of other relational
    // operators from an operator< declared in this fashion.)
}
Run Code Online (Sandbox Code Playgroud)

参加最近的标准会议的人也许可以权衡对提案的反应.

  • https://isocpp.org/blog/2015/11/kona-standards-meeting-trip-report “不透明类型 (P0109R0)。由于对所解决的问题过于复杂而被拒绝。” (2认同)