Ioannis Vranos wrote:
Here is one I would ask:
Is the following code guaranteed to be safe and portable?
cat main.cc
#include <string>
#include <vector>
#include <cstddef>
int main(int argc, char* argv[]) {
using namespace std;
class A {
private:
vector<int> array;
string s;
public:
A(void): array(100) { }
} a;
unsigned char* p = reinterpret_cas t<unsigned char *>(&a);
unsigned char* v = new unsigned char[sizeof(a)];
for(size_t i = 0; i < sizeof(a); ++i)
v[i] = p[i];
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
Why would you ask such a question?
What would you expect it to reveal?
There is *no* guarantee that any code will be safe and portable.
Your code appears to comply with the ANSI/ISO C++ standard.
It will port almost everywhere.
Your code has no outputs and no persistent effects
and is "safe" in that sense.
My first suspicion is that you don't really know what you are doing.
I would be reluctant to accept any offer of employment
that you might make.
Perhaps you were simply attempting to be "too clever".
That's a common mistake for both programmers and managers.
If you want to test an applicant's C++ skills,
ask them to write a simple C++ program
or ask them to submit examples of C++ programs that they have written.
Don't test for understanding of subtle features of the language
unless you really need a C++ language lawyer and,
if you hire a C++ language lawyers,
don't expect them to be very productive.
You *will* be disappointed.