问题 constexpr标准仿函数的正确用法是什么?


请使用以下代码:

#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

关于 可能重复 flag,我不相信这是重复的,因为这个问题是问为什么这个规则存在,而我的问题更多是关于首先发现规则以及哪个编译器在执行中是正确的。此外,所谓的副本的答案不回答我的问题。


12710
2018-03-12 21:46


起源

它适用吗? constexpr Op<uint8_t> op{}; ? - Daniel Jour
@DanielJour是的,谢谢!问题已更新。 - OMGtechy
可能重复 为什么C ++需要用户提供的默认构造函数来默认构造一个const对象? - Oktalist


答案:


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

如果程序要求对象的默认初始化 const - 合格的类型 TT 应该是具有用户提供的默认构造函数的类类型。

因此,简单地使用默认构造函数是不够的。它还需要由用户提供。 Clang实施了这条规则,这是必需的 constexpr Op<uint8_t> op{};,但gcc没有 - 在这种情况下允许默认初始化。

然而这句话最近刚修改 p0490。新的措辞,追溯适用于C ++ 14,内容如下:

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

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

Op<uint8_t> 是 常量默认-constructible (由于它没有非静态数据成员的微不足道的原因),所以你可以默认初始化它。现在,C ++ 14将允许:

constexpr Op<uint8_t> op;

实际上,我们从一个gcc bug(允许默认初始化)到一个clang bug(不允许它)。


9
2018-03-12 23:39



因此,根据我的理解,C ++ 14标准将被更改以反映这一新的措辞,或者这将针对未来版本(例如C ++ 1z)? (我不熟悉缺陷过程) - OMGtechy
@OMGtechy C ++ 14不会 变 因此,但是当委员会将其描述为C ++ 14中的缺陷时,实现可能在以C ++ 14模式编译时应用新规则。 - Oktalist