Reputation: 1882
I'm writing a smart ptr template which is intended to be instantiated only for a given base class and its subclasses, which provides boost::shared_ptr-like implicit conversions to variants MyPtr<U>
of the template MyPtr<T>
has long as the conversion is valid from T* to U* (valid base class and const-compatible).
This was working fine in vs2005, but not with g++ on linux so my colleague changed it there, but doing so it broke the const-correctness.
My problem is that I want to unit test that some conversions are not valid (assign MyPtr<const T>
to MyPtr<T>
for example), resulting in the file not compiling! But you can't have a file not compiling in the solution...
If there some VS-specific #pragma or some SFINAE trick that could test a given construct is NOT valid and therefore doesn't compile?
Thanks, --DD
Upvotes: 2
Views: 174
Reputation: 116744
You could run the command line compiler, cl
, which is pretty easy to set up, and capture its error message output.
No makefile, and hardly any info from the solution/project. Just the include path and a source file. At it's simplest you just need a program that "reverses" the exit code of another program:
#include <sstream>
#include <stdlib.h>
int main(int argc, char *argv[])
{
std::ostringstream command;
for (int n = 1; n < argc; n++)
command << argv[n] << " ";
return (system(command.str().c_str()) == EXIT_SUCCESS)
? EXIT_FAILURE : EXIT_SUCCESS;
}
It simply reconstitutes the arguments passed to it into (omitting its own name) and exits the resulting command line, and then returns success if it fails and failure if it succeeds. That is enough to fool Visual Studio or make
.
Technically the reconstituted command line should quote the arguments, but that will only be necessary if you are insane enough to put spaces in your build directory or source file names!
Upvotes: 1
Reputation: 5105
If a given argument (in your case 'const T') would satisfy all the other unit tests except this one you're looking for, isn't it the sign that it would be a perfectly acceptable case? In other words, if it works, why forbid it?
Upvotes: 0