Delete function with message

190 views
Skip to first unread message

Guillaume Racicot

unread,
Jul 5, 2016, 4:23:10 PM7/5/16
to ISO C++ Standard - Future Proposals
Hi there, I got an idea while writing a library. There are multiple cases where I got this:

template<typename T, std::enable_if_t<std::is_polymorphic<T>::value, int> = 0>
void doThings(T) {
   
// ...
}

template<typename T, std::enable_if_t<!std::is_polymorphic<T>::value, int> = 0>
void doThings(T) {
   
static_assert(!std::is_same<T, T>::value, "T must not be polymorphic.");
}

This code gives me clear error messages, nice. But there's a catch: You can't use SFINAE to check if the function is callable:

template<typename T>
auto callDoThingsMaybe(T t) -> decltype(doThings(t)) {
    doThings
(t);
}

template<typename T>
auto callDoThingsMaybe(T t) -> decltype(doOtherThings(t)) {
    doOtherThings
(t);
}

Where doOtherThings(T) only exists if T is polymorphic.

This code won't work, because  doThings(T) is not a SFINAE failure, and calling it will result in a hard error. If I would remove the overload that throws the static assert, SFINAE will work, but we will loose the nice error message.

I would like to make a proposal to enable sfinae with nice error messages.

Look at this alternative:

template<typename T, std::enable_if_t<!std::is_polymorphic<T>::value, int> = 0>
void doThings(T) = delete("T must not be polymorphic."); // proposed syntax

This will follow deleted function rules and give a nice error message when it is called. SFINAE will still work, and the compiler will still print nice error messages for users.

What do you think?

Jim Porter

unread,
Jul 5, 2016, 5:06:14 PM7/5/16
to std-pr...@isocpp.org
On 7/5/2016 3:23 PM, Guillaume Racicot wrote:
> I would like to make a proposal to enable sfinae with nice error messages.
[snip]
> What do you think?

Isn't this what Concepts will do? You could constrain your function to
take types meeting a "Polymorphic" concept and, if you pass something
that's not polymorphic, the compiler would say something like
"constraint not satisfied because T is not a Polymorphic type".

- Jim


Ville Voutilainen

unread,
Jul 5, 2016, 5:06:28 PM7/5/16
to ISO C++ Standard - Future Proposals
I think having such a facility would be immensely useful. One
food-for-thought point, though:
why not do it with an attribute?

template<typename T, std::enable_if_t<!std::is_polymorphic<T>::value, int> = 0>
[[diagnose("T must not be polymorphic.")]]
void doThings(T) = delete;

Ville Voutilainen

unread,
Jul 5, 2016, 5:14:56 PM7/5/16
to ISO C++ Standard - Future Proposals
Correct. The reason why I envisioned the attribute approach is that I
envisioned a general
facility that would be "implementation, if you're going to issue a
diagnostic here, please
include the following text in the diagnostic".

One might also think of talking to the Contracts proposal authors
about means to do compile-time
contracts. It seems like the current proposal focuses on run-time contracts:
http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0380r0.pdf

Guillaume Racicot

unread,
Jul 5, 2016, 5:22:06 PM7/5/16
to ISO C++ Standard - Future Proposals
It would even better with concepts. It would actually look like this:

void doThing(auto&) = delete("For some reason X, you must send a Container")

void doThing(Container&) { /* ... */ }

It will provide more information for the users and give insights of what should be done to make things work.

Jim Porter

unread,
Jul 5, 2016, 5:24:21 PM7/5/16
to std-pr...@isocpp.org
On 7/5/2016 4:14 PM, Ville Voutilainen wrote:
> Correct. The reason why I envisioned the attribute approach is that I
> envisioned a general
> facility that would be "implementation, if you're going to issue a
> diagnostic here, please
> include the following text in the diagnostic".

If this could be expanded to a recommendation to the compiler: "please
don't include a lot of extraneous error information", I'd be happy to
see it. Errors in nested template instantiations can get awfully
verbose, and I haven't come up with a good general-purpose way to keep
them brief.

- Jim

Guillaume Racicot

unread,
Jul 5, 2016, 5:29:39 PM7/5/16
to ISO C++ Standard - Future Proposals
Indeed, it could be an attribute, but I think it feels nicer to get it directly in the delete syntax, as it is only an extension of the delete syntax.
I get the point tough, and one could easily write an implementation as a compiler extension with that.

I'm in the process of writing the proposal on github, I will include this alternative syntax. Thanks!

Guillaume Racicot

unread,
Jul 5, 2016, 7:23:04 PM7/5/16
to ISO C++ Standard - Future Proposals
Here's the git repo of the proposal. Any contribution are welcome!

T. C.

unread,
Jul 6, 2016, 7:36:17 PM7/6/16
to ISO C++ Standard - Future Proposals
I'm fairly sure I've seen something like this before...



Ville Voutilainen

unread,
Jul 6, 2016, 7:48:50 PM7/6/16
to ISO C++ Standard - Future Proposals
On 7 July 2016 at 02:36, T. C. <rs2...@gmail.com> wrote:
> I'm fairly sure I've seen something like this before...
>
> ...ah yes, http://wg21.link/N4186


Indeed, and it's also
https://cplusplus.github.io/EWG/ewg-active.html#152

Guillaume Racicot

unread,
Jul 8, 2016, 1:18:10 PM7/8/16
to ISO C++ Standard - Future Proposals
No way!! Even the same syntax!
Reply all
Reply to author
Forward
0 new messages