3

g++ 6.1.0 (Debian パッケージ バージョン 6.1.1-1、 flags -std=c++17 -fconcepts) を使用して C++ の概念の実験を始めたところですが、次の簡略化された例では理解できないエラー メッセージが表示されました。

#include <iterator>
#include <vector>
#include <iostream>

template <typename B>
concept bool ContextualBool = requires(B b) {
  { bool(b) };
  { !b } -> bool;
};

template <typename It>
concept bool InputIterator = requires(const It iconst, const It jconst, It i) {
  typename std::iterator_traits<It>::reference;
  typename std::iterator_traits<It>::value_type;
  { iconst == jconst } -> ContextualBool;
  { iconst != jconst } -> ContextualBool;
  { *i } -> typename std::iterator_traits<It>::reference;
  { ++i } -> It&;
  { *i++ } -> typename std::iterator_traits<It>::value_type;
};

template <typename P, typename Arg>
concept bool Predicate = requires(P pred, Arg x) {
  { pred(x) } -> ContextualBool;
};

template <typename P, typename It>
concept bool InputPredicate = requires {
  typename std::iterator_traits<It>::reference;
  requires Predicate<P, typename std::iterator_traits<It>::reference>;
};

/* Version 1 */
/*
InputIterator{I}
bool all_of(I begin, I end, InputPredicate<I> pred) {
*/
/* Version 2 */
/*
bool all_of(InputIterator begin, InputIterator end,
Predicate<typename std::iterator_traits<InputIterator>::reference> pred) {
*/
/* Version 3 */
bool all_of(InputIterator begin, InputIterator end,
            InputPredicate<InputIterator> pred) {
  while (begin != end) {
    if (!pred(*begin))
      return false;
    ++begin;
  }
  return true;
}

int main() {
  std::vector<int> v { 1, 2, 3, 4, 5 };
  if (all_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; }))
    std::cout << "All elements of v are even\n";
  return 0;
}

このコードでは、バージョン 1 とバージョン 2 の両方が正常にコンパイルされ、期待される実行結果が得られます。ただし、バージョン 3 では、次のエラー メッセージが表示されます。

/tmp/concepts_repr.cpp: In function ‘int main()’:
/tmp/concepts_repr.cpp:56:70: error: no matching function for call to ‘all_of(std::vector<int>::iterator, std::vector<int>::iterator, main()::<lambda(int)>)’
       if (all_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; }))
                                                                      ^
/tmp/concepts_repr.cpp:44:10: note: candidate: template<class auto:1, class auto:2, class auto:3, class auto:4>  requires predicate( InputIterator<auto:1>) and predicate(InputPredicate<auto:2, auto:1>) and predicate(InputIterator<auto:3>) and predicate(InputPredicate<auto:4, auto:3>) bool all_of(auto:1, auto:1, auto:4)
     bool all_of(InputIterator begin, InputIterator end,
          ^~~~~~
/tmp/concepts_repr.cpp:44:10: note:   template argument deduction/substitution failed:
/tmp/concepts_repr.cpp:56:70: note:   couldn't deduce template parameter ‘auto:2’
       if (all_of(v.begin(), v.end(), [](int n) { return n % 2 == 0; }))
                                                                      ^

エラー メッセージから、何らかの理由で、生成されたテンプレートで InputIterator および InputPredicate テンプレート パラメーターの重複したバージョンが生成されているように見えます。特にバージョン2が機能したことを考えると、なぜこれが起こるのかわかりません。cppreference.com のステートメントを誤解していますか: 「同等の制約付き型指定子によって導入されたすべてのプレースホルダーには、同じ発明されたテンプレート パラメーターがあります」? それとも、これはおそらく gcc のバグですか?

4

1 に答える 1

0

私が収集できるものから、概念は概念名の概念ではなく、型名で機能します。コードのバージョン 3 には がありますがInputPredicate<InputIterator>、これInputIteratorは概念名であり、型名ではありません。したがって、コンパイルの失敗。

本当に必要なのは、InputIterator概念がbeginandの型で動作することendです。これは、関数シグネチャを少し変更することで実現できます。

bool all_of(InputIterator begin, InputIterator end,
        InputPredicate<decltype(begin)> pred) {
  ...
}

または、テンプレート パラメーターを明示的に指定し、require 句を使用してそれらに制約を設定することもできます。

template<typename It, typename P>
  requires InputIterator<It> && InputPredicate<P, It>
bool all_of(It begin, It end, P pred) {
  ...
}

私は実際には後者の構文を好みます。これにより、推定されるテンプレート パラメーターに対する意図した制約がより明確になると思います。

于 2016-05-21T22:42:05.617 に答える