E
er
Hello,
Let's begin with a library that defines:
// A_HPP
namespace library{
class A{};
A::value_type foo(const A& a){...}
}
// B_HPP
namespace library{
class B{};
B::value_type foo(const B& b){...}
}
and I want to extend it, but non intrusively i.e. outside of namespace
library. I proceed like this:
template<typename T> struct is_library : mpl::false_{}; and for each X
in {A,B},
// FWD_A_HPP
#include A_HPP
template<> struct is_library<X> : mpl::true_;
// FWD_FOO
namespace extension{
typename lazy_enable_if<is_library<X>,mpl::identity<typename
X::value_type> >::type
foo(const X& x){
return library::foo(x)
}
}
And I then define my own classes :
// C_HPP
namespace extension{
struct C{};
typename C::value_type foo(const C& c){...};
}
The limitation of the above, to use it say in main.cpp, is that I have
to
#include FWD_A_HPP
before
#include FWD_FOO
Correct?
If the number of dependencies is large, keeping track of which files
comes before other becomes tedious so I guess the standard solution is
to add a forwarding declaration inside FWD_FOO,
namespace library{
typename A::value_type foo(const A& a);
}
just before extension::foo. Correct?
The problem is further complicated by the fact that A, B and C, in my
actual problem, have template e.g.
template<typename T> struct A;
template<typename T,typename U> struct B;
but the function signature is always
templat<typename X>
typename X::value_type foo(const X&);
On some compilers (MSVC), the above does not work. I get a compile
error : ambiguous call to overloaded function.
What is the best practice for this kind of problem?
Let's begin with a library that defines:
// A_HPP
namespace library{
class A{};
A::value_type foo(const A& a){...}
}
// B_HPP
namespace library{
class B{};
B::value_type foo(const B& b){...}
}
and I want to extend it, but non intrusively i.e. outside of namespace
library. I proceed like this:
template<typename T> struct is_library : mpl::false_{}; and for each X
in {A,B},
// FWD_A_HPP
#include A_HPP
template<> struct is_library<X> : mpl::true_;
// FWD_FOO
namespace extension{
typename lazy_enable_if<is_library<X>,mpl::identity<typename
X::value_type> >::type
foo(const X& x){
return library::foo(x)
}
}
And I then define my own classes :
// C_HPP
namespace extension{
struct C{};
typename C::value_type foo(const C& c){...};
}
The limitation of the above, to use it say in main.cpp, is that I have
to
#include FWD_A_HPP
before
#include FWD_FOO
Correct?
If the number of dependencies is large, keeping track of which files
comes before other becomes tedious so I guess the standard solution is
to add a forwarding declaration inside FWD_FOO,
namespace library{
typename A::value_type foo(const A& a);
}
just before extension::foo. Correct?
The problem is further complicated by the fact that A, B and C, in my
actual problem, have template e.g.
template<typename T> struct A;
template<typename T,typename U> struct B;
but the function signature is always
templat<typename X>
typename X::value_type foo(const X&);
On some compilers (MSVC), the above does not work. I get a compile
error : ambiguous call to overloaded function.
What is the best practice for this kind of problem?