оператор == () используя шаблон шаблона - PullRequest
0 голосов
/ 14 декабря 2018

РЕДАКТИРОВАТЬ: Пролог: я жертва моего собственного невежества, а также ночной кодировки.


I 'я пишу шаблонный класс, используя шаблон шаблона.У него есть итератор, что означает, что мне нужно предоставить соответствующий шаблон operator==().Вот где у меня проблемы.

Ниже приведен типичный пример кода:

#include <iostream>
#include <typeinfo>

using namespace std;

namespace detail {
  template <typename T> class foo {};
  template <typename T> class bar {};
}

template <template<class> class A, template<class> class B>
struct basic_thing {
  template <typename T> using target_type = A<B<T>>;

  target_type<float> fmember;
  target_type<int>   imember;

  struct iterator {
    bool equal (const iterator& other) { return true; }
  };

  iterator begin () { return iterator{}; }
  iterator end   () { return iterator{}; }
};

template <template<class> class A, template<class> class B>
bool operator== (const typename basic_thing<A, B>::iterator& lhs, const typename basic_thing<A, B>::iterator& rhs) {
  return lhs.equal(rhs);
}

int main ()
{
  using Thing = basic_thing<detail::foo, detail::bar>;

  Thing t;
  cout << typeid(t.fmember).name() << endl;
  cout << typeid(t.imember).name() << endl;

  bool b = (t.begin() == t.end());

  return 0;
}

Моя цель здесь - предоставить составной способ определения basic_thing::target_type, и этот шаблон работает для этой цели.Но я застрял в том, как объявить operator==() для basic_thing::iterator.Либо это не очень просто, либо есть что-то очевидное, что я скучаю.(Вероятно, последний.)

g ++ - 7.4.0 с -std=c++11 производит следующее:

foo.cc: In function 'int main()':
foo.cc:39:23: error: no match for 'operator==' (operand types are 'basic_thing<detail::foo, detail::bar>::iterator' and 'basic_thing<detail::foo, detail::bar>::iterator')
   bool b = (t.begin() == t.end());
             ~~~~~~~~~~^~~~~~~~~~
foo.cc:27:6: note: candidate: template<template<class> class A, template<class> class B> bool operator==(const typename basic_thing<A, B>::iterator&, const typename basic_thing<A, B>::iterator&)
 bool operator== (const typename basic_thing<A, B>::iterator& lhs, const typename basic_thing<A, B>::iterator& rhs) {
      ^~~~~~~~
foo.cc:27:6: note:   template argument deduction/substitution failed:
foo.cc:39:32: note:   couldn't deduce template parameter 'template<class> class A'
   bool b = (t.begin() == t.end());
                            ^

Каковы некоторые правильные способы сделать это?Возможно ли это даже при использовании шаблонов?

1 Ответ

0 голосов
/ 14 декабря 2018

Более простой способ - создать его внутри структуры напрямую (в качестве функции-члена или друга):

template <template<class> class A, template<class> class B>
struct basic_thing {
  // ...
  struct iterator {
    bool equal (const iterator& other) { return true; }

    bool operator ==(const iterator& rhs) const;
    // friend bool operator ==(const iterator& lhs, const iterator& rhs);
  };
};

С

template <template<class> class A, template<class> class B>
bool operator== (const typename basic_thing<A, B>::iterator& lhs,
                 const typename basic_thing<A, B>::iterator& rhs);

A и B не являютсяфраншиза (слева от ::).

, поэтому вызывается только уродливый способ:

bool b = operator==<detail::foo, detail::bar>(t.begin(), t.begin());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...