 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
francis_r Guest
|
Posted: Fri Jul 14, 2006 2:14 am Post subject: Testing for code that should not compile? |
|
|
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
|
Posted: Sat Jul 15, 2006 2:09 am Post subject: Re: Testing for code that should not compile? |
|
|
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
|
Posted: Wed Jul 19, 2006 4:21 am Post subject: Re: Testing for code that should not compile? |
|
|
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
|
Posted: Thu Jul 20, 2006 1:12 am Post subject: Re: Testing for code that should not compile? |
|
|
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 |
|
 |
|
|
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
|
|