17

In a generic function I use the following idiom,

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    ... other stuff here...
    using std::copy;
    copy(first, second, d_first);
}

do_something is a generic function that shouldn't know anything specific about any other libraries (except perhaps std::).

Now suppose I have several iterator in my namespace N.

namespace N{

  struct itA{using trait = void;};
  struct itB{using trait = void;};
  struct itC{using trait = void;};

}

An I want to overload copy for these iterators in this namespace. Naturally I would do:

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

However when I call do_something with N::A, N::B or N::C argument I get "ambiguous call to copy" even though these are in the same namespace as N::copy.

Is there a way to win over std::copy in the context of the original function above?

I though that if I put constrains over the template arguments then N::copy would be preferred.

namespace N{
    template<class SomeN1, class SomeN2, typename = typename SomeN1::trait>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

but it doesn't help.

What other workarounds can I try for the generic call to copy to prefer to a copy in the namespace of arguments rather than std::copy.

Complete code:

#include<iostream>
#include<algorithm>
namespace N{
  struct A{};
  struct B{};
  struct C{};
}

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using std::copy;
    copy(first, second, d_first); // ambiguous call when It is from namespace N (both `std::copy` and `N::copy` could work.
}

int main(){
    N::A a1, a2, a3;
    do_something(a1, a2, a3); 
}

A typical error message is

error: call of overloaded ‘copy(N::A&, N::A&, N::A&)’ is ambiguous


Am I right to think that C++ Concepts will help here by preferring function calls with more contraints than less constraints?

alfC
  • 14,261
  • 4
  • 67
  • 118
  • 1
    Not a dupe, but [related](https://stackoverflow.com/q/2953684/9593596). – lubgr Jan 28 '19 at 11:19
  • 1
    Obviously the error comes from always doing `using std::copy;`. – Matthieu Brucher Jan 28 '19 at 11:20
  • @MatthieuBrucher, sure, but that (`using std::X; X(a, b, c);`) is a common way to make `copy` work with things like pointers (that don't have a namespace themselves). – alfC Jan 28 '19 at 11:25
  • 3
    @alfC Your `N::copy` is also a function template that has no arguments associated with namespace `N`. Hence, it is not better than `std::copy` for the purpose of overload resolution. – Maxim Egorushkin Jan 28 '19 at 11:27
  • No, the standard way is to use `std::copy` directly :p – Matthieu Brucher Jan 28 '19 at 11:27
  • it's crude but...a macro might work. redefine copy to another name. – Garr Godfrey Jan 28 '19 at 11:32
  • 2
    @MatthieuBrucher, if I use `std::copy` then there is no chance the special version of `copy` is called. The only way would be to overload `std::copy` (in the namespace `std`) which I don't know if it is allowed. – alfC Jan 28 '19 at 11:33
  • How about using the SFINAE template, and in `do_something`, use `using namespace std; using N::copy;`? – geza Jan 28 '19 at 11:34
  • And by having the `using`, you make sure that you have an ambiguity with ADL for your namespace fucntions. – Matthieu Brucher Jan 28 '19 at 11:34
  • @geza please, no using namepsace std. – Matthieu Brucher Jan 28 '19 at 11:35
  • @MaximEgorushkin , that's a good point, actually if I have three versions of copy that force to use the types explicitly then I don't have this problem `auto copy(SomeN1 first, SomeN1 last, A d_first)` `auto copy(SomeN1 first, SomeN1 last, B d_first)` `auto copy(SomeN1 first, SomeN1 last, C d_first)`. – alfC Jan 28 '19 at 11:35
  • 1
    @geza, the problem is that `do_something` is a template function that doesn't know about the namespace `N` or the library `N`. – alfC Jan 28 '19 at 11:36
  • @MatthieuBrucher: what's the problem with it in this scenario? It is local to a function, which only contains `copy`. I don't see any problems with this. – geza Jan 28 '19 at 11:37
  • 1
    @alfC Yep, introducing an argument from namespace `N` enables ADL. – Maxim Egorushkin Jan 28 '19 at 11:40
  • 1
    @MaximEgorushkin the issue is then `int*` doesn't work anymore with the class. – Matthieu Brucher Jan 28 '19 at 11:44
  • @alfC I might have a solution to decouple the do something function and the namespace/iterators; will write an answer in a few hours – Daniel Jour Jan 28 '19 at 20:50

7 Answers7

4

You can declare copy() as a public friend function in your iterator classes. This works kind of as a replacement for partial specialization (which is impossible for functions), so that they will be preferred by overload resolution as they are more specialized:

#include <iostream>
#include <algorithm>
#include <vector>

namespace N
{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first)
    {
        std::cout << "here" << std::endl;
        return d_first;
    }

    template <class T>
    struct ItBase
    {
        template <class SomeN2>
        friend SomeN2 copy(T first, T last, SomeN2 d_first)
        {
            return N::copy(first, last, d_first);
        }
    };

    struct A : ItBase<A>{};
    struct B : ItBase<B>{};
    struct C : ItBase<C>{};
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using std::copy;
    copy(first, second, d_first);
}

int main(){
    N::A a1, a2, a3;
    std::cout << "do something in N:" << std::endl;
    do_something(a1, a2, a3); 

    std::vector<int> v = {1,2,3};
    std::vector<int> v2(3);
    std::cout << "do something in std:" << std::endl;
    do_something(std::begin(v), std::end(v), std::begin(v2));
    for (int i : v2)
        std::cout << i;
    std::cout << std::endl;
}

See this demo to verify that it works.

I introduced a common base class that declares the necessary friends for all of your iterators. So, instead of declaring a tag, as you tried, you just have to inherit from ItBase.

Note: If N::copy() is supposed to work with only these iterators in N, it might not be needed anymore as these friend functions will be publicly visible in N anyway (as if they were free functions).


Update:

In the comments, it has been suggested, if the iterators in N have a common base class anyway, to just declare N::copy with this base class, e.g.

namespace N
{
    template <class SomeN2>
    SomeN2 copy(ItBase first, ItBase last, SomeN2 d_first) { ... }
}

Unfortunately, this would have the opposite effect of the desired one: std::copy will always be preferred over N::copy because if you pass an instance of A, it would have to be downcasted in order to match N::copy while no cast is required for std::copy. Here you can see that obviously std::copy is tried to be called (which gives an error because N::A lacks some typedefs).

So, you cannot leverage a common base class for the signature of N::copy. The one and only reason I used one in my solution was to avoid duplicate code (having to declare the friend function in every iterator class). My ItBase does not participate in overload resolution at all.

Note, however, if your iterators happen to have some common members (whether derived from some common base class or not is not important) that you want to use in your implementation of N::copy, you can just do that with my solution above like so:

namespace N
{
    template <class T>
    struct ItBase
    {
        template <class SomeN2>
        friend SomeN2 copy(T first, T last, SomeN2 d_first)
        {
            first.some_member();
            last.some_member();
            return d_first;
        }
    };

    struct A : ItBase<A>{ void some_member() {} };
    struct B : ItBase<B>{ void some_member() {} };
    struct C : ItBase<C>{ void some_member() {} };
}

See here how it works.


On the same lines, if A, B, C have common behavior then it could be possible to replaced them by common template class parameterized in some way.

namespace N
{
    template <class T, int I>
    struct ItCommon
    {
       ...
    };
    using A = ItCommon<double,2>;
    using B = ItCommon<int, 3>;
    using C = ItCommon<char, 5>;
}
...
namespace N{
    template<class T, int I, class Other>
    SomeN2 copy(ItCommon<T, I> first, ItCommon<T, I> last, Other){
        ...
    }
} 

Since this (non-friend) copy function is definitely more constrained than the std::copy and because of ADL, it will have high priority when one of the arguments belongs to the N namespace. Also, being a non-friend, this copy function is an optional component.

alfC
  • 14,261
  • 4
  • 67
  • 118
sebrockm
  • 5,733
  • 2
  • 16
  • 39
  • This is an interesting solution and the friend in the base class allows have the coupling between the iterators and the algorithm declared only once. The problem with this is that the `N::iterators` will have to know of all possible algorithms that can implemented `copy`, `fill`, `accumulate`, etc. – alfC Jan 29 '19 at 14:29
  • @alfC So you are saying that `copy` was just an example and actually you want to do this with several std algorithms? In that case you have to implement them anyway, so just put them into `ItBase` directly. Or am I misunderstanding you? – sebrockm Jan 29 '19 at 15:14
  • Yes, the point is that these iterators will need to know about any special function in advance. Besides that point, this a good alternative design. – alfC Jan 29 '19 at 16:04
  • BTW, I think public or private doesn't make a difference here for a friend function. friend functions seem to be always accesible (public). – alfC Jan 29 '19 at 16:08
  • your answer inspired me for this yet-another-alternative, https://stackoverflow.com/a/54425412/225186 – alfC Jan 29 '19 at 16:22
  • @alfC "the point is that these iterartors will need to know about any special function in advance" I don't see the issue with this. You have to provide these "special function's" implementation *somewhere* anyhow. So, if they are especially designed for all iterators inheriting from `ItBase`, why not group them all together inside of `ItBase` as friends? – sebrockm Jan 29 '19 at 19:59
  • sure, that is a possible design. The idea is that I would implement `N::copy` in terms of more fundamental operations (e.g. members) on the iterators A, B, and C. (but still fundamentally different from `std::copy` to need a special implementation.) – alfC Jan 29 '19 at 21:07
  • @alfC Ok I see. But if these members are all different for A, B, and C, then you'll have to provide 3 different implementations anyway. If all iterators have the same members, you can access them via the template param `T` that you passed to `ItBase`. Same goes for non member functions or whatever else you have in mind for your implementation of `copy`. – sebrockm Jan 29 '19 at 21:21
  • That's right, I think this is the point, if A, B, C have common interfaces/structure (enough to implement all in the same way in `N::copy`) that means that there might be a common base class OR a common template implementation of A, B, C (parameterized by other class). In that case the arguments of `N::copy` can be either `(iter_base, ...)` or `template T1 ... (iter, `. Both have higher precedence than `std::copy` in the example. If you agree, please add a note to your answer. – alfC Jan 30 '19 at 02:02
  • 1
    @alfC see my update. If I misunderstood what you mean, please update your question as well to ellaborate, as this seems to be an important detail in order to give you the answer that you are looking for – sebrockm Jan 30 '19 at 09:48
3

One possible solution is to use another function template name and type discriminators to allow argument-dependent name lookup to find the associated function in the namespace of the arguments:

template<class T> struct Tag {};
template<class T> Tag<void> tag(T const&);

template<class It1, class It2>
void mycopy(It1 first, It1 second, It2 d_first, Tag<void>) {
    std::cout << "std::copy\n";
}

template<class It1, class It2>
void mycopy(It1 first, It1 second, It2 d_first) {
    mycopy(first, second, d_first, decltype(tag(first)){}); // Discriminate by the type of It1.
}

namespace N{

    struct itA{using trait = void;};
    Tag<itA> tag(itA);

    template<class It1, class It2>
    void mycopy(It1 first, It1 second, It2 d_first, Tag<itA>) {
        std::cout << "N::mycopy\n";
    }
}

int main() {
    char* p = 0;
    mycopy(p, p, p); // calls std::copy

    N::itA q;
    mycopy(q, q, q); // calls N::mycopy
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
2

This seems to fulfill your requirements:

namespace SpecCopy {

template <typename A, typename B, typename C>
void copy(A &&a, B &&b, C &&c) {
    std::copy(std::forward<A>(a), std::forward<B>(b), std::forward<C>(c));
}

}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using namespace SpecCopy;
    copy(first, second, d_first);
}

Basically, it depends on ADL. If no function found by ADL, then it will use SpecCopy::copy, which is a wrapper to std::copy.


So, if you do:

N::A a1, a2, a3;
do_something(a1, a2, a3);

Then do_something will call N::copy.


If you do:

std::vector<int> a1, a2;
do_something(a1.begin(), a1.end(), a2.begin());

Then do_something will call SpecCopy::copy, which will call std::copy.


If you do:

int *a1, *a2, *a3;
do_something(a1, a2, a3);

Then same thing happens as before: do_something will call SpecCopy::copy, which will call std::copy.

geza
  • 28,403
  • 6
  • 61
  • 135
  • 1
    I don't think this solves the issue ... it ties the general `do_something` to the special code for the iterators ... – Daniel Jour Jan 28 '19 at 16:31
  • @DanielJour: Can you give an example or more explanation? I don't understand what you mean. – geza Jan 28 '19 at 16:58
  • @DanielJour , geza, as DanielJour says, `do_something` will need to know about the `SpecCopy` library/namespace. and if you include `SpecCopy` with `do_something` then there is no customization, `N::copy` is never called in this way. – alfC Jan 28 '19 at 19:03
  • @alfC: Hmm, I thought that you want an ADL based solution. If iterator is in `namespace N`, and you have a `copy` function in `namespace N`, then with this solution, `N::copy` will be called. – geza Jan 28 '19 at 19:16
  • @alfC: I've edited my answer so you can see what I mean. – geza Jan 28 '19 at 19:29
2

In c++ 11 you could use tag dispatch. If you can make a little change to your custom iterators things will be a bit simpler to implement.

#include <iostream>
#include <algorithm>
#include <vector>
#include <type_traits>

// indicates that the type doesn't have a tag type (like pointers and standard iterators)
struct no_tag{};

namespace detail 
{
    template <typename T>
    auto tag_helper(int) -> typename T::tag;

    template <typename>
    auto tag_helper(long) -> no_tag;
}

// get T::tag or no_tag if T::tag isn't defined.
template <typename T>
using tag_t = decltype(detail::tag_helper<T>(0));

namespace N
{
    struct my_iterator_tag {};
    struct A{ using tag = my_iterator_tag; };
    struct B{ using tag = my_iterator_tag; };
    struct C{ using tag = my_iterator_tag; };
}

namespace N
{
    template<class SomeN1, class SomeN2>
    SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, no_tag)
    {
        std::cout << "calling std::copy\n";
        return std::copy(std::forward<SomeN1>(first), std::forward<SomeN1>(last), std::forward<SomeN2>(d_first));
    }

    template<class SomeN1, class SomeN2>
    SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, my_iterator_tag)
    {
        // your custom copy        
        std::cout << "custom copy function\n";
        return {};
    }

    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first)
    {
        return copy_helper(std::forward<SomeN1>(first), std::forward<SomeN1>(last), std::forward<SomeN2>(d_first), tag_t<SomeN1>{});
    }
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first)
{
    N::copy(first, second, d_first);
}

int main()
{
    N::A a1, a2, a3;
    std::cout << "using custom iterator: ";
    do_something(a1, a2, a3); 

    std::cout << "using vector iterator: ";
    std::vector<int> v;
    do_something(std::begin(v), std::end(v), std::begin(v));

    std::cout << "using pointer: ";
    int* ptr = new int[10];
    do_something(ptr, ptr + 5, ptr);

    return 0;
}

First we change our custom iterators to have a tag type (maybe change the name to avoid confusion with iterator_category). tag can be any type you want, it just has to match the type you use as tag in copy_helper.

Next, we define a type that allows us to access this tag type, or to fall back to a default type if tag doesn't exist. This will help us distinguish between our custom iterators and standard iterators and pointers. The default type I use is no_tag. The tag_t provides us with this functionality by using SFINAE and overload resolution. We call the function tag_helper(0) which has two declarations. The first one returns T::tag while the second one returns no_tag. Calling tag_helper(0) will always try to use the first version because int is a better match for 0 than long. This means we will always try to access T::tag first. However if this isn't possible (T::tag is not defined) SFINAE kicks in and skipps tag_helper(int) selecting tag_helper(long).

Finally, we just have to implement a copy function for each tag (I called it copy_helper) and another copy function as a wrap around for convinience (I used N::copy). The wrapper function then creates the proper tag type and calls the correct helper function.

Here is a live example.

Edit

If you move the code around a bit you can disconnect namespace N and rely on ADL:

#include <iostream>
#include <algorithm>
#include <vector>
#include <type_traits>

// indicates that the type doesn't have a tag type (like pointers and standard iterators)
struct no_tag{};

namespace detail 
{
    template <typename T>
    auto tag_helper(int) -> typename T::tag;

    template <typename>
    auto tag_helper(long) -> no_tag;
}

// get T::tag or no_tag if T::tag isn't defined.
template <typename T>
using tag_t = decltype(detail::tag_helper<T>(0));

namespace N
{
    struct my_iterator_tag {};
    struct A{ using tag = my_iterator_tag; };
    struct B{ using tag = my_iterator_tag; };
    struct C{ using tag = my_iterator_tag; };

    template<class SomeN1, class SomeN2>
    SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, my_iterator_tag)
    {
        // your custom copy        
        std::cout << "custom copy function\n";
        return {};
    }
}

template<class SomeN1, class SomeN2>
SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, no_tag)
{
    std::cout << "calling std::copy\n";
    return std::copy(std::forward<SomeN1>(first), std::forward<SomeN1>(last), std::forward<SomeN2>(d_first));
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first)
{
    copy_helper(std::forward<It1>(first), std::forward<It1>(second), std::forward<It2>(d_first), tag_t<It1>{});
}

int main()
{
    N::A a1, a2, a3;
    std::cout << "using custom iterator: ";
    do_something(a1, a2, a3); 

    std::cout << "using vector iterator: ";
    std::vector<int> v;
    do_something(std::begin(v), std::end(v), std::begin(v));

    std::cout << "using pointer: ";
    int* ptr = new int[10];
    do_something(ptr, ptr + 5, ptr);

    return 0;
}
Timo
  • 9,269
  • 2
  • 28
  • 58
  • Thank you, but `do_something` will need to know about the `N` namespace, which might be in a disconnected library. – alfC Jan 28 '19 at 19:00
  • @alfC Well you could also move the code from `copy` directly to `do_something` and include the `copy_helper` with the `no_tag` tag in the same library. Then you can rely on ADL again. Your disconnected library has to implement the `copy_helper` with its according tag. – Timo Jan 28 '19 at 19:38
  • The Edit is the closest to an answer to the question so far. Note aside, I think the `forward` is useless unless you pass arguments as `It1&& first`. – alfC Jan 28 '19 at 19:58
1

OK, building on @paler123, but without checking for an existing type, but checking if It1 is a pointer instead:

namespace N{
  struct A{};
  struct B{};
  struct C{};
}

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1, SomeN1, SomeN2 c){
        std::cout << "here" << std::endl;
        return c;
    }
}
template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    if constexpr (std::is_pointer_v<It1>) {
        std::copy(first, second, d_first);
    }
    else
    {
        copy(first, second, d_first);
    }
}


int main(){
    N::A a1, a2, a3;
    do_something(a1, a2, a3); 

    int* b1, *b2, *b3;

    do_something(b1, b2, b3); 
}

Still C++17, but in the case of pointers, we go through the explicit std::copy otherwise, we rely on ADL.

In general, your issue is a design problem. You want to use std::copy for all cases, except for objects from N, and in that case, you hope that ADL will work. But as you forced std::copy, you remove the option for proper ADL. You can't have everything and you have to redesign your code.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Thanks for the option. Pointer types are just an example, other (non-std iterators) will not work now. In other words this forces all iterators and pointer-like iterators to implement `copy` for them, even if `std::copy` would be fine for them. – alfC Jan 28 '19 at 11:52
  • Yes, that's the problem, I agree. The problem you have, once again, is a design problem. You want to try to rely on ADL in some cases, but not in others. Not going to work. – Matthieu Brucher Jan 28 '19 at 11:55
1

(These notes are now integrated in my edit to @sebrockm's answer)


For discussion I will write an answer to my own question with an alternative option.

It is not very nice because it needs to wrap all the N:: classes in another template class (called wrap here). The good thing is that do_something nor the N classes need to know about the special N::copy. The price is that the main caller has to explicitly wrap the N:: classes which is ugly but which is fine from the point of view of coupling because this is the only code that should know about the whole system.

#include <iostream>
#include <algorithm>
#include <vector>

namespace N{
    struct A{};
    struct B{};
    struct C{};
}

namespace N{

    template<class S> struct wrap : S{};

    template<class SomeN1, class SomeN2>
    SomeN2 copy(wrap<SomeN1> first, wrap<SomeN1> last, wrap<SomeN2> d_first)
    {
        std::cout << "here" << std::endl;
        return d_first;
    }
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using std::copy;
    copy(first, second, d_first);
}

int main(){
    N::wrap<N::A> a1, a2, a3;
    std::cout << "do something in N:" << std::endl;
    do_something(a1, a2, a3); 

    std::vector<int> v = {1,2,3};
    std::vector<int> v2(3);
    std::cout << "do something in std:" << std::endl;
    do_something(std::begin(v), std::end(v), std::begin(v2));
    for (int i : v2)
        std::cout << i;
    std::cout << std::endl;
}
alfC
  • 14,261
  • 4
  • 67
  • 118
  • That or the approach with the friend function is probably the best You can get; there aren't any more rules for ADL best viable candidate selection that one could leverage. – Daniel Jour Jan 30 '19 at 13:13
-2

Suggest you have a look at the very powerful new Boost.HOF library.

This function does exactly what you want:

#include <boost/hof.hpp>

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    namespace hof = boost::hof;

    auto my_copy = hof::first_of(
    [](auto first, auto second, auto d_first) -> decltype(N::copy(first, second, d_first))
    {
        return N::copy(first, second, d_first);
    },
    [](auto first, auto second, auto d_first) -> decltype(std::copy(first, second, d_first))
    {
        return std::copy(first, second, d_first);
    });
    my_copy(first, second, d_first);
}

hof::first_of will select the first lambda whose return type is deduced to be the result type of a legal expression.

alfC
  • 14,261
  • 4
  • 67
  • 118
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • 1
    This looks like an interesting library. However the problem is that `do_something` still needs to know about the library/namespace `N`. – alfC Jan 28 '19 at 20:18
  • @alfC I see. It wasn't clear to me that you're after a general solution for calling `copy`. – Richard Hodges Jan 29 '19 at 00:20