views:

101

answers:

3

hello.

I have run into broken compiler, which does not allow exceptions to inherit from std::exception (nvcc 3.0).

so had to create workaround:

struct exception {
    explicit exception(const char* message) {
        what_ = message;
    }
    virtual const char *what() const throw() { return what_; }
    operator std::exception() const {
        return std::runtime_error(what_);
    }
private:
    const char* what_;
};

struct configuration_error : exception {
    configuration_error(const char* message)
        : exception(message) {}
};

is there something I am going to lose by not inheriting from exception, instead providing cast? is what necessary in the above case?

thank you

+2  A: 

catch (std::exception) will not catch your exception, and you cannot dynamic_cast your exception to std::exception either. If your program never does either of those things, you're fine.

Personally I would not use a compiler this broken, though.

Zack
well, it's only one right now for cuda programming
aaa
A: 

You should upgrade to CUDA 3.1.

Tom
A: 

If you're trying to create an instance of something derived from std::exception on the device, it won't work because the std::exception constructor would need to be called, and that is not a device function.

Throwing and catching your own type is probably the solution. Since it probably will never make it back to host code, though (C++ doesn't really handle throwing multiple exceptions in parallel), compatibility with std::exception shouldn't be too much of an issue.

interfect