문제

Suppose I have a library and multiple projects dependent on that library. The library headers already has some partial class specializations. I want to allow each dependent project to override with its own partial specializations. I need to achieve this all statically for performance reasons. Some simplified code is below.

Library code:

template <class A, class B, class Enable=void>
struct Widget;

struct Foo
{
};

template <class B>
struct Widget<Foo, B>
{
};

User code:

template <class B>
struct DoSpecialize;

template <class B>
struct Widget<Foo, B, enable_if< DoSpecialize<B> >::type
{
};

The problem here is we end up with multiple definitions of the same specialization. I think we need a disable_if<> somewhere. How could we avoid this?

도움이 되었습니까?

해결책

I'd suggest to solve it with separating the layers. The library has it's own specializations and the user can overwrite them if needed. If this is acceptable, the following is the library code:

namespace impl
{
    template <class A, class B, class Enable=void>
    struct Widget;
}

template <class A, class B, class Enable=void>
struct Widget : impl::Widget< A, B > {};

struct Foo
{
};

namespace impl
{
    template <class B>
    struct Widget<Foo, B>
    {
    };
}

and the user code stays as it is.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top