0

Let's say I have a noncopyable class and I want to provide a customized error message when users try to copy.

struct A {
    A() = default;

private:
    [[deprecated("customized message")]]
    A(const A&);

    [[deprecated("customized message")]]
    A& operator=(const A&);
};

I cannot use =delete for them, because compiler would only complain they are deleted. So I tried the transitional way: private non-defined functions.

Compiler will show

warning: 'A::A(const A&)' is deprecated: customized message [-Wdeprecated-declarations]

It works but not that good, because the word deprecated sounds like allowed-but-discouraged rather than disallowed simply.

My another try is

struct Customized_Message {
    Customized_Message() = default;
    Customized_Message(const Customized_Message&) = delete;
    Customized_Message& operator=(const Customized_Message&) = delete;
};
struct A : Customized_Message {
    A() = default;
};
<source>:17:11: error: use of deleted function 'A::A(const A&)'
   17 |     A b = a;
      |           ^
<source>:11:8: note: 'A::A(const A&)' is implicitly deleted because the default definition would be ill-formed:
   11 | struct A : Customized_Message {
      |        ^
<source>:11:8: error: use of deleted function 'Customized_Message::Customized_Message(const Customized_Message&)'
<source>:8:5: note: declared here
    8 |     Customized_Message(const Customized_Message&) = delete;
      |     ^~~~~~~~~~~~~~~~~~

It works but looks not elegant ([Edit] because the message is not short, and compiler will replicate it as above).

Is there a better way to provide a customized error message? Thank you.

  • is it some class specific text or just something along the line of "this class cannot be copied" ? – 463035818_is_not_an_ai Oct 22 '22 at 10:01
  • btw "elegant" is purely subjective. Your second appraoch is similar to `boost::non_copyable`. It is widely used hence cannot be too bad – 463035818_is_not_an_ai Oct 22 '22 at 10:03
  • Might your custom error message be more confusing then the standard error message the compiler emits which developers are already familiar with ? – Richard Critten Oct 22 '22 at 10:09
  • Thanks for the comments, the customized message would be something like "XXX is disallowed, please use YYY, detailed documentation is in ZZZ." – Home of the Brave Oct 22 '22 at 10:09
  • its not clear why you think it is "not elegant". Is it because you have to write a class to be inherited from? Is it because you have to write another class for a different custom message? How about having a member named `XXX_is_disallowed_please_use_YYY_detailed_documentation_is_in_ZZZ` that is not copyable? – 463035818_is_not_an_ai Oct 22 '22 at 10:10
  • If it's non-defined, compiler might go on but linker will complain, so I don't see the point there. Besides that, your approach #2 is understandable I think. – lorro Oct 22 '22 at 10:11
  • 1
    well, more seriously, you cannot solve the issue of a user not reading documentation with this one instance of a compiler error message a user gets when they do something wrong. And you should not. A user is used to their compilers error messages, and they should have read documentation before writing the wrong code. Reality is that this wont be the case always, but you cannot solve that by turning a well readable and clear error message into a custom one – 463035818_is_not_an_ai Oct 22 '22 at 10:12
  • take a look at `boost::non_copyable`, though afaik this predates `= delete` (which you should prefer to declare but not define, a compiler error is already so much clearer than a linker error) – 463035818_is_not_an_ai Oct 22 '22 at 10:16
  • fwiw, I think this question is missing details, because it cannot be answered without knowing what you consider as "elegant" or "better", hence my vote to close. When you have a solution and you do not like it you need to explain why you don't like it rather than assume that it is obvious, it isnt. – 463035818_is_not_an_ai Oct 22 '22 at 10:18
  • Sorry, I just edited it. – Home of the Brave Oct 22 '22 at 10:18
  • How to close a question? – Home of the Brave Oct 22 '22 at 10:19

0 Answers0