By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,406 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,406 IT Pros & Developers. It's quick & easy.

g++ 3.4 question

P: n/a
I believe that the following is valid c++:

#include <iostream>

template <typename CMP>
class Comparator
{
bool operator()(int x,int y)
{
return CMP(x,y);
};
};

typedef Comparator<&(operator <)> LessComp;

int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};
Gnu c++ 3.4 disagrees with me, because the operator < is interpreted as
an opening bracket for a template argument that isn't ever completed. A
similar problem occurs with operator >, except that this is seen as the
closing bracket (even though it is inside an opened round bracket!).
Older versions of g++ are happy, but 3.4 is generally much closer to the
standard, so maybe I am wrong about this code being valid.

Can anyone enlighten me?

Many thanks

Derek
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Derek Long wrote:
I believe that the following is valid c++:
I can only guess from your code what you a re trying to achieve, because
you didn't tell us.
I think what you want to do is to instantiate a template with an address
of a function (operator<) you want to call in the template body. That
doesn't work. As far as I know, you may only use constant integral
values to instantiate a template.

#include <iostream>

template <typename CMP>
class Comparator
{
bool operator()(int x,int y)
{
return CMP(x,y);
};
};
Your CMP is not supposed to be a type. Based on my assumption, it's
supposed to be a function. Which doesn't work, since you would have to
take the address of the function at runtime, but templates are
instantiated at compile time. You can't instantiate a template with a
variable which value the compiler doesn't know at compile time.

typedef Comparator<&(operator <)> LessComp;

See above.
int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};
Gnu c++ 3.4 disagrees with me, because the operator < is interpreted as
an opening bracket for a template argument that isn't ever completed. A
similar problem occurs with operator >, except that this is seen as the
closing bracket (even though it is inside an opened round bracket!).
Older versions of g++ are happy, but 3.4 is generally much closer to the
standard, so maybe I am wrong about this code being valid.

Can anyone enlighten me?

Many thanks

Derek


I like the idea though :)
Jul 23 '05 #2

P: n/a
Derek Long wrote:
I believe that the following is valid c++:

#include <iostream>

template <typename CMP>
CMP is a TYPE !
class Comparator
{
bool operator()(int x,int y)
{
return CMP(x,y);
Instantiation of a type - must be calling the constructor ?
};
};

typedef Comparator<&(operator <)> LessComp;
operator < needs to be defined.

int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};
Gnu c++ 3.4 disagrees with me, because the operator < is interpreted as
an opening bracket for a template argument that isn't ever completed. A
similar problem occurs with operator >, except that this is seen as the
closing bracket (even though it is inside an opened round bracket!).
Older versions of g++ are happy, but 3.4 is generally much closer to the
standard, so maybe I am wrong about this code being valid.

Can anyone enlighten me?


I don't think this is what you want - however - try this:

#include <iostream>

template <typename CMP, CMP v>
class Comparator
{
public:
bool operator()(int x,int y)
;/* {
return CMP(x,y);
};
*/
};

struct X;

bool operator<( const X& a, const X& b );
typedef bool (*T)( const X& a, const X& b );
T F = & operator<;
typedef Comparator< T, & operator< > LessComp;

int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};

It's more likely you're looking for a functor.

template <typename T>
struct Less
{
bool operator()( const T& a, const T& b )
{
return a < b;
}
};

Jul 23 '05 #3

P: n/a
By the way, don't reinvent the wheel:

-----------
#include <iostream>

int main()
{
std::less<int> less;
std::cout << std::boolalpha << less(2,3) << std::endl;
}
-----------

output:
true

Regards,
Matthias
Jul 23 '05 #4

P: n/a
matthias_k <no****@digitalraid.com> wrote in
news:ct*************@news.t-online.com:
Derek Long wrote:
I believe that the following is valid c++:


I can only guess from your code what you a re trying to achieve,
because you didn't tell us.
I think what you want to do is to instantiate a template with an
address of a function (operator<) you want to call in the template
body. That doesn't work. As far as I know, you may only use constant
integral values to instantiate a template.


What?! How do you think std::vector works? Y'know:
class MyCustomClass {
/* Put in all appropriate constructors */
};

std::vector<MyCustomClass> somevec;
MyCustomClass is most certainly _not_ a "constant integral value"....
Jul 23 '05 #5

P: n/a
Andre Kostur wrote:
What?! How do you think std::vector works? Y'know:
class MyCustomClass {
/* Put in all appropriate constructors */
};

std::vector<MyCustomClass> somevec;
MyCustomClass is most certainly _not_ a "constant integral value"....


I'm not talking about TYPES, I'm talking about VALUES.
Something like this:

template< int value >
void print_value()
{
std::cout << value << std::endl;
}

print_value<5>(); // print 5
print_value<1>(); // print 1

Regards,
Matthias
Jul 23 '05 #6

P: n/a
matthias_k <no****@digitalraid.com> wrote in news:ct*************@news.t-
online.com:
Andre Kostur wrote:
What?! How do you think std::vector works? Y'know:
class MyCustomClass {
/* Put in all appropriate constructors */
};

std::vector<MyCustomClass> somevec;
MyCustomClass is most certainly _not_ a "constant integral value"....


I'm not talking about TYPES, I'm talking about VALUES.
Something like this:

template< int value >
void print_value()
{
std::cout << value << std::endl;
}

print_value<5>(); // print 5
print_value<1>(); // print 1


OK, in your example you need a constance integral value. But the OP
wasn't using an int in the template line, it was:

template <typename CMP>

So the OP's template needs a type, not a value....
Jul 23 '05 #7

P: n/a
Andre Kostur wrote:
matthias_k <no****@digitalraid.com> wrote in news:ct*************@news.t-
online.com:

Andre Kostur wrote:
What?! How do you think std::vector works? Y'know:
class MyCustomClass {
/* Put in all appropriate constructors */
};

std::vector<MyCustomClass> somevec;
MyCustomClass is most certainly _not_ a "constant integral value"....


I'm not talking about TYPES, I'm talking about VALUES.
Something like this:

template< int value >
void print_value()
{
std::cout << value << std::endl;
}

print_value<5>(); // print 5
print_value<1>(); // print 1

OK, in your example you need a constance integral value. But the OP
wasn't using an int in the template line, it was:

template <typename CMP>

So the OP's template needs a type, not a value....


That's correct, but I think that wasn't what he really wanted to do. He
instantiated the template with an address to a function (operator<).
That is hardly a type.
Maybe the OP can clarify what his intention was.

Regards,
Matthias
Jul 23 '05 #8

P: n/a
Just a pedantic comment- you might have derived your functors from
std::binary_function ("Effective STL" by Scott Meyers explains why).

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:wM********************@speakeasy.net...
Derek Long wrote:
I believe that the following is valid c++:

#include <iostream>

template <typename CMP>


CMP is a TYPE !
class Comparator
{
bool operator()(int x,int y)
{
return CMP(x,y);


Instantiation of a type - must be calling the constructor ?
};
};

typedef Comparator<&(operator <)> LessComp;


operator < needs to be defined.

int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};
Gnu c++ 3.4 disagrees with me, because the operator < is interpreted as
an opening bracket for a template argument that isn't ever completed. A
similar problem occurs with operator >, except that this is seen as the
closing bracket (even though it is inside an opened round bracket!).
Older versions of g++ are happy, but 3.4 is generally much closer to the
standard, so maybe I am wrong about this code being valid.

Can anyone enlighten me?


I don't think this is what you want - however - try this:

#include <iostream>

template <typename CMP, CMP v>
class Comparator
{
public:
bool operator()(int x,int y)
;/* {
return CMP(x,y);
};
*/
};

struct X;

bool operator<( const X& a, const X& b );
typedef bool (*T)( const X& a, const X& b );
T F = & operator<;
typedef Comparator< T, & operator< > LessComp;

int main()
{
LessComp less;
std::cout << less(2,3) << "\n";
return 0;
};

It's more likely you're looking for a functor.

template <typename T>
struct Less
{
bool operator()( const T& a, const T& b )
{
return a < b;
}
};

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.