f



Problem with std::set

The documentation of std::set says that the second optional argument of 
a template set instantion should be a function object or a function pointer.
In the example below I am trying to create a set object using a function 
pointer.
I compiled the code with:
$ g++ -std=c++11 Set.cpp
and got the compiler error that there was a type mismatch in the second 
argument.
Please help.
Thank you,
Joe

========================= Set.cpp =============================
#include <iostream>
#include <set>

class X
{
private:
   int x;
public:
   X(int a) : x(a) {}
   friend bool Comp(const X &, const X &);
};

bool Comp(const X &xobj1, const X &xobj2)
{
   return xobj1.x < xobj2.x;
}

int main()
{
   std::set<X, Comp> SetOfX; // <== use function pointer

   return 0;
}
0
Joseph
12/22/2016 10:03:23 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
581 Views

Similar Articles

[PageSpeed] 59

On 22.12.2016 23:03, Joseph Hesse wrote:
> The documentation of std::set says that the second optional argument of
> a template set instantion should be a function object or a function
> pointer.
> In the example below I am trying to create a set object using a function
> pointer.
> I compiled the code with:
> $ g++ -std=c++11 Set.cpp
> and got the compiler error that there was a type mismatch in the second
> argument.
> Please help.
> Thank you,
> Joe
>
> ========================= Set.cpp =============================
> #include <iostream>
> #include <set>
>
> class X
> {
> private:
>   int x;
> public:
>   X(int a) : x(a) {}
>   friend bool Comp(const X &, const X &);
> };
>
> bool Comp(const X &xobj1, const X &xobj2)
> {
>   return xobj1.x < xobj2.x;
> }
>
> int main()
> {
>   std::set<X, Comp> SetOfX; // <== use function pointer
>
>   return 0;
> }

The second template argument needs to be a /type/.

You could use `decltype(&Comp)` if you feel unsure about function types.

When you do this, using a simple freestanding function, you need to pass 
it as a constructor argument. That's because a raw function pointer type 
doesn't say anything about which function it is. While a class type does 
say all there is to know: it's that class' operator().


Cheers!,

- Alf

0
Alf
12/22/2016 10:25:26 PM
On 12/22/2016 04:25 PM, Alf P. Steinbach wrote:
> On 22.12.2016 23:03, Joseph Hesse wrote:
>> The documentation of std::set says that the second optional argument of
>> a template set instantion should be a function object or a function
>> pointer.
>> In the example below I am trying to create a set object using a function
>> pointer.
>> I compiled the code with:
>> $ g++ -std=c++11 Set.cpp
>> and got the compiler error that there was a type mismatch in the second
>> argument.
>> Please help.
>> Thank you,
>> Joe
>>
>> ========================= Set.cpp =============================
>> #include <iostream>
>> #include <set>
>>
>> class X
>> {
>> private:
>>   int x;
>> public:
>>   X(int a) : x(a) {}
>>   friend bool Comp(const X &, const X &);
>> };
>>
>> bool Comp(const X &xobj1, const X &xobj2)
>> {
>>   return xobj1.x < xobj2.x;
>> }
>>
>> int main()
>> {
>>   std::set<X, Comp> SetOfX; // <== use function pointer
>>
>>   return 0;
>> }
>
> The second template argument needs to be a /type/.
>
> You could use `decltype(&Comp)` if you feel unsure about function types.
>
> When you do this, using a simple freestanding function, you need to pass
> it as a constructor argument. That's because a raw function pointer type
> doesn't say anything about which function it is. While a class type does
> say all there is to know: it's that class' operator().
>
>
> Cheers!,
>
> - Alf
>
Thank you. I should have known that when instantiating a template, the 
stuff that goes between < and > have to be types.
std::set<X, bool (*)(const X &, const X &)> SetOfX(Comp);
fixes it when I want to use function pointers.
0
Joseph
12/22/2016 11:16:21 PM
On 22/12/2016 22:03, Joseph Hesse wrote:
> The documentation of std::set says that the second optional argument of
> a template set instantion should be a function object or a function
> pointer.
> In the example below I am trying to create a set object using a function
> pointer.
> I compiled the code with:
> $ g++ -std=c++11 Set.cpp
> and got the compiler error that there was a type mismatch in the second
> argument.
> Please help.
> Thank you,
> Joe
>
> ========================= Set.cpp =============================
> #include <iostream>
> #include <set>
>
> class X
> {
> private:
>   int x;
> public:
>   X(int a) : x(a) {}
>   friend bool Comp(const X &, const X &);
> };
>
> bool Comp(const X &xobj1, const X &xobj2)
> {
>   return xobj1.x < xobj2.x;
> }
>
> int main()
> {
>   std::set<X, Comp> SetOfX; // <== use function pointer
>
>   return 0;
> }

Just add an operator< to your class and forget about the template parameter.

/Flibble
0
Mr
12/22/2016 11:22:10 PM
Reply: