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

M

Marcel Müller

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
 
A

Alf P. Steinbach

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() ); }


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
 
V

Victor Bazarov

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
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top