[cfe-dev] Determining whether Container<T>()==Container<T>() can compile?

Mailing List Email mailinglistx at gmail.com
Fri May 24 06:21:28 PDT 2013


It can be done yourself, or am I misunderstanding your question?
Example:

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

template<typename T, typename U>
struct IsEqComparable
{
private:
    typedef struct { char dmy; } yes;
    typedef struct { char dmy[2]; } no;

    template<typename T2, typename U2>
    static auto test(void*) -> typename std::enable_if<std::is_same<
            decltype(std::declval<T2>() == std::declval<U2>()),
            decltype(std::declval<T2>() == std::declval<U2>())
        >::value, yes>::type;

    template<typename T2, typename U2> static no test(...);
public:
    static const bool value = sizeof(test<T, U>(nullptr)) == sizeof(yes);
};

struct bar {};

int main()
{
    if (IsEqComparable<std::vector<int>, std::vector<int>>::value)
        std::cout << "Yes!\n";
    else
        std::cout << "No!\n";
    if (IsEqComparable<std::vector<int>, bar>::value)
        std::cout << "Yes!\n";
    else
        std::cout << "No!\n";
    return 0;
}


On 24 May 2013 10:41, Stephen Kelly <steveire at gmail.com> wrote:

>
> Hi there,
>
> Code such as
>
>   std::vector<A> veca, vecb;
>   veca == vecb;
>
> can only compile if A()==A() can compile, if you'll forgive me taking some
> descriptive shortcuts.
>
> I tried writing a template to determine if a type can be equality-compared
> for the purpose of type-erasure:
>
>  http://thread.gmane.org/gmane.comp.lib.qt.devel/11120
>
> Adding auto and trailing return type seems to be helpful:
>
>  http://thread.gmane.org/gmane.comp.lib.qt.devel/11120/focus=11157
>
> Would the suggestion from Olivier Goffart be something that could be
> applied
> to libcxx to make it possible to write such a template for its stl
> container
> implementations?
>
> Does anyone else have any other ideas for something that would work with
> todays stl implementations?
>
> Thanks,
>
> Steve.
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130524/b65e81d4/attachment.html>


More information about the cfe-dev mailing list