Re: Best practices for forward declaring a template function
* er:
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){...}
}
value_type is undefined.
Assuming you meant to define on in both class A and 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)
}
}
What's the point?
And I then define my own classes :
// C_HPP
namespace extension{
struct C{};
typename C::value_type foo(const C& c){...};
}
value_type is not defined.
There is no relation to the preceding explanation.
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?
No.
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?
Doesn't make sense so far.
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;
So, now you're saying that all you've described so far has been *different* from
your problem.
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.
Error on line 42.
What is the best practice for this kind of problem?
Check out the FAQ item on how to post a question about code that Does Not Work.
Cheers & hth.,
- Alf