compile time dependencies

Y

yurec

Hi

I can't compile

//#include <boost/function.hpp>

class boost::function;

UIStl * LoadStlFromAnotherProject(const CString & i_project_name,
boost::function<bool (MSAXReader &,UIObject *)> i_func_compare);


1. Parameter and return types need only to be forward-declared.
(Herb Sutter , Exceptional C++: 47 Engineering Puzzles, Programming
Problems, and Solutions, Item 26)

Can anybody tell me correct syntax?

Thanks,

Yurij
 
Z

Zeppe

yurec said:
Hi

I can't compile

//#include <boost/function.hpp>

class boost::function;

UIStl * LoadStlFromAnotherProject(const CString & i_project_name,
boost::function<bool (MSAXReader &,UIObject *)> i_func_compare);


1. Parameter and return types need only to be forward-declared.
(Herb Sutter , Exceptional C++: 47 Engineering Puzzles, Programming
Problems, and Solutions, Item 26)

Can anybody tell me correct syntax?

You have to include the header, because you are instantiating a template
class.

Regards,

Zeppe
 
B

Barry

yurec said:
Hi

I can't compile

//#include <boost/function.hpp>

class boost::function;

UIStl * LoadStlFromAnotherProject(const CString & i_project_name,
boost::function<bool (MSAXReader &,UIObject *)> i_func_compare);


1. Parameter and return types need only to be forward-declared.
(Herb Sutter , Exceptional C++: 47 Engineering Puzzles, Programming
Problems, and Solutions, Item 26)

Can anybody tell me correct syntax?
namespace NS {
template <class T> class Func;
}

void f(NS::Func<int>& f) {}
// ^^ reference or pointer

int main() { }
 
Y

yurec

You have to include the header, because you are instantiating a template
class.

Regards,

Zeppe- Hide quoted text -

- Show quoted text -


Could you kindly explain wy do we need detailed info about class when
instantiating template class while it's enough declaration when using
non-template class?
 
M

Matthias Buelow

yurec said:
Could you kindly explain wy do we need detailed info about class when
instantiating template class while it's enough declaration when using
non-template class?

A template is a sort of compiler macro; the compiler can't expand the
macro (= instantiate the template) when it doesn't know about it yet.
 
Y

yurec

A template is a sort of compiler macro; the compiler can't expand the
macro (= instantiate the template) when it doesn't know about it yet.

ok, seems very clear. thanks.
 
B

Barry

Zeppe said:
You have to include the header, because you are instantiating a template
class.

It's not necessary

<std>
14.7.3/10
A template-id that names a class template explicit specialization that
has been declared but not defined can be used exactly like the names of
other incompletely-defined classes (3.9). [Example:
template<class T> class X; // X is a class template
template<> class X<int>;
X<int>* p; // OK: pointer to declared class X<int>
X<int> x; // error: object of incomplete class X<int>
—end example]
</std>


here is how what the op want,
anyway, I don't suggest this :)

<code>
namespace boost {
template <class, class>
class function;
}

#include <boost/function.hpp>

void invoke(boost::function<int(int)>& f) {
f(10);
}

#include <iostream>

int f(int i) { std::cout << i << std::endl; return i; }

int main() {
boost::function<int(int)> fobj(f);
invoke(fobj);
}
</code>
 
T

Tadeusz B. Kopec

Hi

I can't compile

//#include <boost/function.hpp>

class boost::function;

UIStl * LoadStlFromAnotherProject(const CString & i_project_name,
boost::function<bool (MSAXReader &,UIObject *)> i_func_compare);


1. Parameter and return types need only to be forward-declared.
(Herb Sutter , Exceptional C++: 47 Engineering Puzzles, Programming
Problems, and Solutions, Item 26)

Can anybody tell me correct syntax?

There are two problems:
1) Compiler should fail on:
class boost::function;

because the proper syntax for declaring _class_ in namespace is
namespace boost
{
class function;
}

2) Unfortunately boost::function is not a class but a class template. And
you need to know the number of template parameters it takes to forward
declare it. Finding it out might be painful and you are not guaranteed
the number of parameters won't change in next versions. So it's much
better to include the header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top