Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's wrong with a class with only private functions?

65 views
Skip to first unread message

Marcel Müller

unread,
Dec 8, 2013, 6:54:37 PM12/8/13
to
I have a code snippet that crates a warning with gcc (older version):


template <class Element,
class Key,
int(*Comparer)(const Key& key, const Element& elem)>
class sorted_vector_own
{ // ...
};

class File
{ // ...
};

struct Parameters
{private:
static int CompareMeasurement(const char*const& key, const File& data);
public:
typedef sorted_vector_own<File,
const char*,
&Parameters::CompareMeasurement>
MeasurementSet;
MeasurementSet Measurements;
double FreqLow, FreqHigh;
// ...
};


test3.cpp:11: warning: all member functions in class `Parameters' are
private


OK, a compiler could warn about everything it likes. But what could be
the idea of this warning? What is wrong with a class with only private
functions?


Marcel

Alf P. Steinbach

unread,
Dec 8, 2013, 9:15:50 PM12/8/13
to
On 09.12.2013 00:54, Marcel M�ller wrote:
>
> OK, a compiler could warn about everything it likes. But what could be
> the idea of this warning?

The compiler didn't notice that the address of the private function is
exposed, or else the logic of the warning doesn't include such
considerations at all.


> What is wrong with a class with only private functions?

If that's *all* that the class has, then they're useless baggage.

For such a class a compilation warning would be appropriate.

But in the presented code the class also has a public typedef that
exposes one of the private functions, which presumably is why you
reacted to the warning. ;-)

* * *

Here's a demo of how such a typedef allows a function to be called by
any code, illustrating that in spite of the "private:" the function is
publicly accessible with just a *little* more work than usual:


[code]
template< auto()->int > struct Whatever {};

class Not_quite_useless
{
private:
static auto foo() -> int { return 666; }
public:
typedef Whatever<&foo> Access;
};

//----------------------------------------------
#include <iostream>
using namespace std;

template< auto exposed_func() -> int >
void foo( Whatever<exposed_func> )
{ cout << exposed_func() << endl; }

auto main()
-> int
{ foo( Not_quite_useless::Access() ); }
[/code]


By the way, neither Visual C++ 12.0 nor MinGW g++ 4.7.2 warned about the
Not_quite_useless class above.

Cheers & hth.,

- Alf

Victor Bazarov

unread,
Dec 9, 2013, 8:40:37 AM12/9/13
to
On 12/8/2013 6:54 PM, Marcel M�ller wrote:
> I have a code snippet that crates a warning with gcc (older version):
>[...]
> OK, a compiler could warn about everything it likes. But what could be
> the idea of this warning? What is wrong with a class with only private
> functions?

<shrug> About the same thing as with gratuitous warnings.

V
--
I do not respond to top-posted replies, please don't ask
0 new messages