Note that there are some explanatory texts on larger screens.

plurals
  1. POstruct vs class as STL functor when using not2
    primarykey
    data
    text
    <p>Studing STL I have written a a simple program to test functors and modifiers. My question is about the difference aon using CLASS or STRUCT to write a functor and try to operate on it with function adaptors. As far as I understand in C++ the difference beetween CLASS and STRUCT is that in the last case the members are public by default. This is also what I read many times in the answers in this site. So please explain me why this short piece of code will fail to compile even if I declared all members ( just a function overloading () ) public when I try to use the not2 modifier. (I have not tried other modifiers e.g. binders yet)</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;functional&gt; #include &lt;algorithm&gt; using namespace std; template &lt;class T&gt; void print (T i) { cout &lt;&lt; " " &lt;&lt; i; } // In the manual I read: // "In C++, a structure is the same as a class except that its members are public by default." // So if I declare all members public it should work.... template &lt;class T&gt; class mystruct : binary_function&lt;T ,T ,bool&gt; { public : bool operator() (T i,T j) const { return i&lt;j; } }; template &lt;class T&gt; class generatore { public: generatore (T start = 0, T stp = 1) : current(start), step(stp) { } T operator() () { return current+=step; } private: T current; T step; }; int main () { vector&lt;int&gt; first(10); generate(first.begin(), first.end(), generatore&lt;int&gt;(10,10) ); first.resize(first.size()*2); generate(first.begin()+first.size()/2, first.end(), generatore&lt;int&gt;(1,17) ); cout &lt;&lt; "\nfirst :"; for_each (first.begin(), first.end(), print&lt;int&gt;); cout &lt;&lt; "\nFORWARD SORT :"; sort(first.begin(),first.end(),mystruct&lt;int&gt;()); // OK ! even with CLASS for_each (first.begin(), first.end(), print&lt;int&gt;); sort(first.begin(),first.end(),not2(mystruct&lt;int&gt;())); // &lt;--- THIS LINE WILL NOT COMPILE IF I USE CLASS INSTEAD OF STRUCT cout &lt;&lt; "\nBACKWARD SORT :"; for_each (first.begin(), first.end(), print&lt;int&gt;); cout &lt;&lt; endl; } </code></pre> <p>Everithing runs as expected if I use:</p> <pre><code>struct mystruct : binary_function&lt;T ,T ,bool&gt; { public : bool operator() (T i,T j) const { return i&lt;j; } }; </code></pre> <p>Part of the error message I obtain is:</p> <blockquote> <p>g++ struct.cpp<br> /usr/include/c++/4.2.1/bits/stl_function.h:<br> In instantiation of ‘std::binary_negate >’:<br> struct.cpp:52: instantiated from here<br> /usr/include/c++/4.2.1/bits/stl_function.h:116:<br> error: ‘typedef int std::binary_function::first_argument_type’ is inaccessible<br> /usr/include/c++/4.2.1/bits/stl_function.h:338:<br> error: within this context<br> /usr/include/c++/4.2.1/bits/stl_function.h:119:<br> error: ‘typedef int std::binary_function::second_argument_type’ is inaccessible .... </p> </blockquote> <p>Seems that at least in this case a struct is not equivalent to a class with public members, but why ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload