Guaranteeing that a function does *not* exist

S

strnbrg

I'd like to write code that fails to compile if a certain
function *does* exist in a 3rd party library. (This is the
opposite of the well-known problem of wanting compilation to fail if
a certain function does *not* exist.)

Say a library class has been designed to prevent use
of the copy constructor,

class C
{
[stuff...]
private:
C( C const & );
};

I then write client code that assumes a C is never constructed from
another C. But, for extra safety, I'd like to put something in my code
that would cause the compiler to reject my code, should that 3rd party
library ever be changed so as to expose and define the C copy
constructor.

Maybe there's some SFINAE (substitution failure is not an error)
template trick for this...? In any case, I haven't been able to put
one together.


Ted Sternberg
Berkeley, CA, USA
 
V

Victor Bazarov

I'd like to write code that fails to compile if a certain
function *does* exist in a 3rd party library. (This is the
opposite of the well-known problem of wanting compilation to fail if
a certain function does *not* exist.)

Say a library class has been designed to prevent use
of the copy constructor,

class C
{
[stuff...]
private:
C( C const & );
};

I then write client code that assumes a C is never constructed from
another C. But, for extra safety, I'd like to put something in my code
that would cause the compiler to reject my code, should that 3rd party
library ever be changed so as to expose and define the C copy
constructor.

Maybe there's some SFINAE (substitution failure is not an error)
template trick for this...? In any case, I haven't been able to put
one together.

Your "exists in a 3rd party library" is quite vague. What do you mean
by it, exactly? If you don't try using it in your code, yet it exists,
should it compile? How would "existence" be known to the compiler if
it never attempts, to, say, link against that library?

V
 
J

James Aguilar

Do you know what the name of that function? If you do, then you can just define
a function in the same scope and the same arguments, and the program should fail
to compile (you can't have two functions defined that have the same name and
parameters -- make sure to match constness too if it's a member function).

HTH

- JFA1
 
S

strnbrg

Great idea (and breathtakingly simple). I do know the name of the
function; it's the copy constructor, so this will do the trick just
fine. Thanks.
 
D

David White

Great idea (and breathtakingly simple). I do know the name of the
function; it's the copy constructor, so this will do the trick just
fine. Thanks.

How can it do the trick when the function in question is inside a class
definition provided by (I presume) a third party?

DW
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top