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 8 1356
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 :)
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;
}
};
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
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"....
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
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....
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
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; } }; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mohammed Mazid |
last post by:
Can anyone please help me on how to move to the next and previous
question?
Here is a snippet of my code:
Private Sub cmdNext_Click()
End Sub
Private Sub cmdPrevious_Click()
showrecord
|
by: Stevey |
last post by:
I have the following XML file...
<?xml version="1.0"?>
<animals>
<animal>
<name>Tiger</name>
<questions>
<question index="0">true</question>
<question index="1">true</question>
</questions>
|
by: nospam |
last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types,
so, since it seems to me that Partial Types is still in the design or
development stages at Microsoft, I am going to ask...
|
by: Ekqvist Marko |
last post by:
Hi,
I have one Access database table including questions and answers. Now
I need to give answer id automatically to questionID column. But I
don't know how it is best (fastest) to do?
table...
|
by: glenn |
last post by:
I am use to programming in php and the way session and post vars are past
from fields on one page through to the post page automatically where I can
get to their values easily to write to a...
|
by: Rider |
last post by:
Hi, simple(?) question about asp.net configuration..
I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm
first start application the follow message shown.
=========
Server...
|
by: Jeff |
last post by:
In the function below, can size ever be 0 (zero)?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;
clc_assert_not_null(clc_strdup, s);
size = strlen(s) + 1;
|
by: spibou |
last post by:
In the statement "a *= expression" is expression assumed to be
parenthesized ? For example if I write "a *= b+c" is this the same
as "a = a * (b+c)" or "a = a * b+c" ?
|
by: Allan Ebdrup |
last post by:
Hi,
I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix
question is a question where you have several options that can all be rated
according to several possible ratings (from...
|
by: Zhang Weiwu |
last post by:
Hello!
I wrote this:
..required-question p:after {
content: "*";
}
Corresponding HTML:
<div class="required-question"><p>Question Text</p><input /></div>
<div...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |