【问题标题】:What is the proper usage of a constexpr standard functor?constexpr 标准函子的正确用法是什么?
【发布时间】:2017-08-02 21:01:31
【问题描述】:

取以下代码:

#include <iostream>
#include <functional>

template <template<typename> class Op>
auto applyOp(const uint8_t lhs, const uint8_t rhs) {
    constexpr Op<uint8_t> op;

    return op(lhs, rhs);
}

int main() {
    std::cout << +applyOp<std::bit_and>(19, 180) << std::endl;
}

当使用g++ 时,它编译并运行得很好。但是clang++ 会产生错误:

test.cpp:5:27: error: default initialization of an object of const type 'const bit_and<uint8_t>' (aka 'const bit_and<unsigned char>') without a user-provided default constructor
    constexpr Op<uint8_t> op;
                          ^
                            {}
test.cpp:11:19: note: in instantiation of function template specialization 'applyOp<std::bit_and>' requested here
    std::cout << +applyOp<std::bit_and>(19, 180) << std::endl;
                  ^
1 error generated.

所以我看了一下bit_and的源代码:

 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 // terms of the GNU General Public License as published by the
 // Free Software Foundation; either version 3, or (at your option)
 // any later version.

 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.

 // Under Section 7 of GPL version 3, you are granted additional
 // permissions described in the GCC Runtime Library Exception, version
 // 3.1, as published by the Free Software Foundation.

 // You should have received a copy of the GNU General Public License and
 // a copy of the GCC Runtime Library Exception along with this program;
 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 // <http://www.gnu.org/licenses/>.

 /*
  *
  * Copyright (c) 1994
  * Hewlett-Packard Company
  *
  * Permission to use, copy, modify, distribute and sell this software
  * and its documentation for any purpose is hereby granted without fee,
  * provided that the above copyright notice appear in all copies and
  * that both that copyright notice and this permission notice appear
  * in supporting documentation.  Hewlett-Packard Company makes no
  * representations about the suitability of this software for any
  * purpose.  It is provided "as is" without express or implied warranty.
  *
  *
  * Copyright (c) 1996-1998
  * Silicon Graphics Computer Systems, Inc.
  *
  * Permission to use, copy, modify, distribute and sell this software
  * and its documentation for any purpose is hereby granted without fee,
  * provided that the above copyright notice appear in all copies and
  * that both that copyright notice and this permission notice appear
  * in supporting documentation.  Silicon Graphics makes no
  * representations about the suitability of this software for any
  * purpose.  It is provided "as is" without express or implied warranty.
  */

[ ... content omitted ... ]

template<typename _Arg1, typename _Arg2, typename _Result>
     struct binary_function
     {
       /// @c first_argument_type is the type of the first argument
       typedef _Arg1     first_argument_type;

       /// @c second_argument_type is the type of the second argument
       typedef _Arg2     second_argument_type;

       /// @c result_type is the return type
       typedef _Result   result_type;
     };

[ ... content omitted ... ]

#if __cplusplus > 201103L
   template<typename _Tp = void>
     struct bit_and;

   [ ... content omitted ... ]

#endif

   [ ... content omitted ... ]

   // _GLIBCXX_RESOLVE_LIB_DEFECTS
   // DR 660. Missing Bitwise Operations.
   template<typename _Tp>
     struct bit_and : public binary_function<_Tp, _Tp, _Tp>
     {
       _GLIBCXX14_CONSTEXPR
       _Tp
       operator()(const _Tp& __x, const _Tp& __y) const
       { return __x & __y; }
     };

  [ ... content omitted ... ]

据我所知,这里应该生成一个默认构造函数。 有趣的是,错误消息特别要求“用户提供的默认构造函数”,而不仅仅是“默认构造函数”。

将违规行更改为使用统一初始化会导致代码与两个编译器一起工作:

- constexpr Op<uint8_t> op;
+ constexpr Op<uint8_t> op { };

我的问题是,clang++ 要求这些额外的大括号是正确的,还是 g++ 不正确?

额外信息

使用g++ 命令编译:

g++ test.cpp -Werror -Wall -pedantic -std=c++14

使用clang++ 命令编译:

clang++ test.cpp -Werror -Wall -pedantic -std=c++14

运行应用命令:

./a.out

bit_and定义的位置:

/usr/include/c++/6.2.0/bits/stl_function.h

关于possible duplicate 标志,我不认为这是重复的,因为该问题是在询问为什么存在此规则,而我的问题更多是关于首先找出规则以及哪个编译器其执行是正确的。此外,对所谓重复的答案没有回答我的问题。

【问题讨论】:

标签: c++ templates c++14 language-lawyer constexpr


【解决方案1】:

[dcl.init] 中的原始 C++14 措辞要求:

如果程序要求对const 限定类型T 的对象进行默认初始化,则T 应是具有用户提供的默认构造函数的类类型。

因此,仅仅拥有一个默认构造函数是不够的。它还需要由用户提供。 Clang 实现了这个规则,它需要 constexpr Op&lt;uint8_t&gt; op{};,但 gcc 没有 - 在这种情况下允许默认初始化。

然而,这个措辞最近在p0490 中进行了修改。追溯适用于 C++14 的新措辞如下:

如果T 的默认初始化将调用T 的用户提供的构造函数(不是从基类继承),则类类型Tconst-default-constructible 或如果
T 的每个直接非变量非静态数据成员 M 都有一个默认成员初始化器,或者,如果 M 是类类型 X(或其数组),X 是 const-default-可施工,
— 如果T 是具有至少一个非静态数据成员的联合,则恰好一个变体成员具有默认成员初始值设定项,
— 如果T 不是联合,则对于每个具有至少一个非静态数据成员(如果有)的匿名联合成员,恰好一个非静态数据成员具有默认成员初始化器,并且每个可能构造的基类T 是 const-default-constructible。

如果程序要求对 const 限定类型 T 的对象进行默认初始化,则 T 应为 const-default-constructible 类类型或其数组。

Op&lt;uint8_t&gt;const-default-constructible (因为它没有非静态数据成员的微不足道的原因),所以你可以默认初始化它。现在,C++14 将允许:

constexpr Op<uint8_t> op;

实际上,我们从一个 gcc 错误(允许默认初始化)变成了一个 clang 错误(不允许它)。

【讨论】:

  • 所以据我了解,C++14 标准将被更改以反映这个新措辞,或者这将针对未来的版本(例如 C++1z)? (我对缺陷过程不熟悉)
  • @OMGtechy C++14 不会像这样更改,但正如委员会将其描述为 C++14 中的一个缺陷,实现可能会应用新规则在 C++14 模式下编译时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
相关资源
最近更新 更多