C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Testing for code that should not compile?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
francis_r
Guest





PostPosted: Fri Jul 14, 2006 2:14 am    Post subject: Testing for code that should not compile? Reply with quote



Hello newsgroup readers,

Writing some test cases today I wondered if it would be possible to
assert that something does not compile.
For example you want to make sure that a class, say Widget, has no
default constructor. Then you would write:
ASSERT_NO_COMPILE ( Widget(); ) // compile time check

Or may at runtime. I think it's possible to write a Testing Framework
that allows you to pass C++ statements as a string that will be
compiled by a compiler that was fired up by the Testing Framework at
run time.

Tester::CheckNoCompile( "Widget();" ); // Tester should report error
if it compiles on Tester's compiler

Has something like this ever been done? Do you think it's an
interesting idea?

Just curious.

Kind regards,
Francis


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Sat Jul 15, 2006 2:09 am    Post subject: Re: Testing for code that should not compile? Reply with quote



francis_r wrote:
Quote:
Writing some test cases today I wondered if it would be possible to
assert that something does not compile.

What for?

Quote:
For example you want to make sure that a class, say Widget, has no
default constructor. Then you would write:
ASSERT_NO_COMPILE ( Widget(); ) // compile time check

Not sure what it buys you. I've not had to rely on something that
doesn't exist, only on something that does.

Quote:
Or may at runtime.

At runtime *what*?

Quote:
I think it's possible to write a Testing Framework
that allows you to pass C++ statements as a string that will be
compiled by a compiler that was fired up by the Testing Framework at
run time.

That would call for some kind of compiler API. It is absolutely
possible. Compilers are programs and can allow hooking into them.

Quote:
Tester::CheckNoCompile( "Widget();" ); // Tester should report error
if it compiles on Tester's compiler

Has something like this ever been done?

Probably. However, usefulness of it is highly questionable.

Quote:
Do you think it's an
interesting idea?

Not really. Do you?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Richard Smith
Guest





PostPosted: Wed Jul 19, 2006 4:21 am    Post subject: Re: Testing for code that should not compile? Reply with quote



Frederick Gotham wrote:
Quote:
francis_r posted:

ASSERT_NO_COMPILE ( Widget(); ) // compile time check


5.3.3 Sizeof
-----
| The operand is either an expression, which is not evaluated,
| or a parenthesized type-id.
-----

How is that relevant to the question?

I assume what you're suggesting is some use of SFINAE on the argument
of sizeof (which may or may not be legal depending on your
interpretation of section 14.8.2 in the Standard; certainly no compiler
I've used supports it):

// [Code completely untested]
#define ASSERT_NO_COMPILE( EXPR ) \
struct DoAssertNoCompile##__FILE__##__LINE__ { \
typedef char does; \
typedef struct { does x[2]; } doesnt; \
template <std::size_t> struct dummy {}; \
template <class U> static does fn( dummy< sizeof( U(), (EXPR) ) >*
); \
template <class U> static doesnt fn(...); \
static const bool compiles = sizeof( fn<int>(0) ) == sizeof(does);
\
BOOST_STATIC_ASSERT( not compiles ); \
}

But that's largely irrelevant because, as I said, I've never seen a
compiler that will compile this -- and it may well not be legal.

--
Richard Smith


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frederick Gotham
Guest





PostPosted: Thu Jul 20, 2006 1:12 am    Post subject: Re: Testing for code that should not compile? Reply with quote

Richard Smith posted:


Quote:
How is that relevant to the question?


Q. How do I check if an expression is valid?
A. By writing it and trying to compile it. E.g.:

int main()
{
ArbitraryClass().ParticularMemberFunc();

/* If this compiles, then "ArbitraryClass"
does indeed have a member function
called "ParticularMemberFunc".
(Or perhaps a function pointer member,
or perhaps a member object which has
a user-defined function-style operator,
but we won't get into that...) */
}


Q. How do I check if an expression is valid without actually
evaluating the
expression?
A. By writing "sizeof expr" and trying to compile it. E.g.:

int main()
{
sizeof( ArbitraryClass.ParticularMemberFunc(), 1 );

/* If this compiles, then "ArbitraryClass"
does indeed have a member function
called "ParticularMemberFunc". */
}


Here's a template function which will only work with classes which
have a
member function called "Call" which can be invoked without any
arguments:

template<class T>
void Func()
{
sizeof( T().Call(), 1 );
}


struct Arb1 {

void Call() {}
};


struct Arb2 {

void Process() {}
};

int main()
{
Func<Arb1>();

Func<Arb2>(); /* Compile ERROR */
}


G++:

In function `void Func() [with T = Arb2]':
instantiated from here
'struct Arb2' has no member named 'Call'


Quite a nice intelligible error if you ask me.


--

Frederick Gotham

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.