Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add constrained perfect forwarding sample #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add constrained perfect forwarding sample #53

wants to merge 1 commit into from

Conversation

rollbear
Copy link
Contributor

Simple technique for giving the compiler a chance to provide better error messages. I am not aware of any drawbacks, other than lengthening the code a bit.

@sftrabbit
Copy link
Owner

Hey. I've been out of action for a while, so I'm only just catching up on this stuff.

It looks like your sample is really just a combination of the Function template SFINAE sample and the Perfect forwarding sample (well actually, yours uses the SFINAE mechanism required for constructors - maybe we need a sample for that). My intent is for each sample to illustrate a single idea, rather than multiple ideas in combination. Any thoughts?

@rollbear
Copy link
Contributor Author

The implementation is as you say, but the intention is more specific. However, since I failed to get the idea across to you, I certainly wouldn't have succeeded in getting it across to less experienced programmers, so as a minimum I would need to work on the explanation.

The intent is to get a short cut when you want to implement two variants.

In the example there was:

class foo {
public:
  foo(std::string&& s_) : s(std::move(s_)) {}
  foo(const std::string& s_) : s(_) {}
private:
  std::string s;
};

The frequently taught short cut is just straight perfect forwarding:

class foo {
public:
  template <typename U>
  foo(U&& u) : s(std::forward<U>(u)) {}
private:
  std::string s;
};

This works, but if you provide a type to the constructor that a string cannot be constructed from, you get an error message referring to string constructors, which is confusing, given that it was a foo you wanted to construct.

The constraining technique in the PR also gives an error message in that situation, but one saying that there's no suitable constructor for foo, which I find more helpful.

At first I wrote the example using a variadic template, to say any parameters suitable to make a string, but I scrapped that to reduce the complexity.

However, I admit that the construction is a bit esoteric for a perhaps not super important problem, so I'm perfectly happy if you don't think it's suitable for CppSamples, although I personally think it's a fairly neat technique

@sftrabbit
Copy link
Owner

I think the description is actually pretty good. It would only need a bit of tweaking.

I'm almost thinking that this would be best renamed to "Constructor SFINAE", and then we're simply using "constrained perfect forwarding" as the example common case (which we can keep mentioned in the description). The reason for this is that the "forwarding" part of the sample is really covered already by the other perfect forwarding sample, while the novel part of this sample is the constructor SFINAE. What do you think of that idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants