Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator==() using template template


EDIT: Prolog: I'm a victim of my own ignorance and also of late-night coding.


I'm writing a templated class using template template. It has an iterator, which means that I need to provide an appropriately templated operator==(). This is where I'm having trouble.

Representative code sample follows:

#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;
}

My goal here is to provide a composable way to define basic_thing::target_type, and this pattern works for that purpose. But, I'm stuck at how to declare operator==() for basic_thing::iterator. Either this isn't very straightforward, or there's something obvious that I'm missing. (Likely the latter.)

g++-7.4.0 with -std=c++11 produces the following:

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());
                            ^

What are some correct ways to do this? Is it even possible when template templates are involved?

like image 688
smcdow Avatar asked Mar 10 '26 01:03

smcdow


1 Answers

The simpler way is to create it inside the struct directly (as member or friend function):

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);
  };
};

With

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 and B are not deducible (on the left of ::).

so only callable the ugly way:

bool b = operator==<detail::foo, detail::bar>(t.begin(), t.begin());
like image 59
Jarod42 Avatar answered Mar 12 '26 15:03

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!