g++ 3.4 question

D

Derek Long

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
 
M

matthias_k

Derek said:
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 :)
 
M

matthias_k

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
 
G

Gianni Mariani

Derek said:
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;
}
};
 
A

Andre Kostur

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"....
 
M

matthias_k

Andre said:
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
 
A

Andre Kostur

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....
 
M

matthias_k

Andre said:
online.com:




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
 
E

Efrat Regev

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

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

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top