Skip to main content
gamedev.net
🔒 Locked

Template resolve

Started by surabaya Oct 6, 2002 at 9:43 AM 6 replies 1.1k views
Original Post
surabaya
surabaya
Hi I tried:
    
...
Functor func; // a functor object

typedef std::vector<long> Array;
Array arr;
...
std::sort<Array::iterator, Functor&>( arr.begin(), arr.end(), func );

    
Didn't compile on VS.NET -> "too many template arguments." But why? std::sort does have a template version that takes an iterator and a predicate type. Could somebody help? I am trying to avoid passing the predicate by value for efficiency consideration. edit: the template parameters inside the less than greater than brackets were not shown. added the source tag section [edited by - surabaya on October 6, 2002 11:00:31 AM] [edited by - surabaya on October 6, 2002 11:01:54 AM] [edited by - surabaya on October 6, 2002 11:10:54 AM]
---------------------------What a great day to be alive!
surabaya
surabaya
Please help.
---------------------------What a great day to be alive!
Fruny
Fruny
quote:

I am trying to avoid passing the predicate by value for efficiency consideration.



Premature optimisation. What is the size of your functor ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
DirkGerrits
DirkGerrits
STL is built on the fact that most function pointers are small and always takes them by value. If you don''t like that, do this:

std::sort( arr.begin(), arr.end(), boost::cref(func) );

See also: http://www.boost.org/libs/bind/ref.html

Regards,
Dirk Gerrits
Regards,Dirk Gerrits
surabaya
surabaya
quote:

Premature optimisation. What is the size of your functor ?



Agreed. But still I don''t find the reason for the program not to compile.

By the way I still don''t know the functor''s size since I needed to know whether pass-by-ref is possible or not before designing it.

I''ll try the boost solution.

Thanks,
---------------------------What a great day to be alive!
Xai
Xai
pass by ref is NOT possible for functors in the standard librsry, hence the need for boost:cref template ...

the standard supports ref and value versions of functions where absolutely necessary to allow implementation, but not in many cases where it would only be an efficiency issue, because there would then need to be twice as many versions in the standard, each version by ref and by value ...
surabaya
surabaya
quote:

pass by ref is NOT possible for functors in the standard librsry, hence the need for boost:cref template ...




  
template<typename T>
void Func( T arg ) { ...... }

class SomeClass { ........ };

//.....

SomeClass obj;

Func( obj ); // compiles

Func<SomeClass&>( obj ); // compiles and pass by ref as expected


// below doesn''t compile (too many template args")

typedef std::vector<int> Arr;
std::sort<Arr::iterator, SomeClass&>( arr.begin(), arr.end(), obj );



Without bothering to discuss STL, std::sort is basically just another function template; why can''t it take my template arguments just like normal functions?
---------------------------What a great day to be alive!
Fruny
Fruny
That being said, have you tried directly passing a reference instead of forcing the template type, and seen what error you got ?


Functor f;
Functor &rf = f;
std::sort( foo, bar, rf );



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.