I am on the task to migrate the concept of error handling in a C++ class library. Methods that previously simply returned bool (success/fail) shall be modified to return a Result
object which conveys a machine readable error code and a human readable explanation (and some more which does not matter here).
Walking through thousands of lines of code is error prone, therefore I try to get the best support from the compiler for this task.
My result class has - among other member methods - a constructor that constructs the result from a code and an assignment operator for the code:
class Result
{
public:
typedef unsigned long ResultCode;
explicit Result(ResultCode code); // (1)
Result& operator=(ResultCode code); // (2)
};
Remark: I would usually use an enum class for ResultCode
which would solve my problems, but this is not an option. This is because the major design objective was to use Result
in different libraries, each of which shall define its own set of result codes without requiring one big header file that defines all possible result codes for all libraries. In fact, each class shall be able to define local result codes so that the list of possible result codes can be obtained from the classes header. Thus the codes cannot be enumerated in Result
, they must be defined by the classes using the Result
class.
To avoid implicit conversions on
return true;
Statements in the client code, the constructor has been declared explicit. But in nesting method calls, another problem occurs. Say, I have a method
bool doSomething()
{
return true;
}
Which I am using in a function that returns a Result
object. I want to forward result codes of nested calls
Result doSomethingElse
{
Result result = doSomething();
return result;
}
With the current implementation of Result
's assignment operator, this is not going to give me a compiler error - the boolean return value of doSomething() is implicitly converted to unsigned long.
As I have read in the C++ documentation, only constructors and conversion operators may be declared explicit.
My questions
- Why is explicit not allowed for assignment operators or other methods? IMO it would make a lot of sense to allow any method to be explicit as well.
- Are there other solutions to prevent implicit type conversion for the assignment operator?
template<typename T> void operator=(T) = delete;
and keep the one you have. It will use that one in normal case and will try to use deleted method for all other types. – doug65536 Jul 19 '16 at 7:11error: conversion from 'bool' to non-scalar type 'Result' requested
if I try this on coliru, not investigated further. – Jonathan Potter Jul 19 '16 at 7:21enum class
. Your question reads like "I do not want to use a bbq to cook this cake. The recipie says to use an oven at 350 F, and I cannot use a bbq because I am allergic to bees. Why does my oven not work?". You understand how this makes it tricky to answer. I mean, there are simularities between ovens and bbqs, and maybe the allergy to bees is not the real reason to avoid using the oven, and maybe your oven really does not work, but the question seems to belie confusion not describe a problem. – Yakk - Adam Nevraumont Jul 19 '16 at 7:45