Discussion:
[std-proposals] Shortcoming in class template template parameter deduction.
Bengt Gustafsson
2018-11-02 11:31:59 UTC
Permalink
My naive understanding was that a class template with constructors should
be callable as if it was a function, returning an object of an appropriate
type.

This works but excludes the situation where you in the function case would
have to provide a subset of template parameter values yourself. For class
templates you have to provide all or none.

Here is an example:

template<typename T, typename R> void Foo(R r)
{
}

template<typename T, typename R> class Bar {
public:
Bar(R r) {}
};

int main()
{
Foo<int>(3.14); // Works
Bar<int>(3.14); // Error
}


The Bar construction line does not compile on any of the main compilers,
which I think it should, just as Foo call does.

What is the good reason for not making this consistent?


(Godbolt: https://godbolt.org/z/b_L7bE)
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org.
Nicolas Lesser
2018-11-02 11:51:42 UTC
Permalink
See also P1021, "Filling holes in Class Template Argument Deduction".

https://wg21.link/p1021

On Fri, Nov 2, 2018, 12:32 PM Bengt Gustafsson <
Post by Bengt Gustafsson
My naive understanding was that a class template with constructors should
be callable as if it was a function, returning an object of an appropriate
type.
This works but excludes the situation where you in the function case would
have to provide a subset of template parameter values yourself. For class
templates you have to provide all or none.
template<typename T, typename R> void Foo(R r)
{
}
template<typename T, typename R> class Bar {
Bar(R r) {}
};
int main()
{
Foo<int>(3.14); // Works
Bar<int>(3.14); // Error
}
The Bar construction line does not compile on any of the main compilers,
which I think it should, just as Foo call does.
What is the good reason for not making this consistent?
(Godbolt: https://godbolt.org/z/b_L7bE)
--
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq0cyTwdkZDjmqUn3Vd7f1TtBjVmr9qTBAqvXFEf4%3DgU3g%40mail.gmail.com.
Bengt Gustafsson
2018-11-02 12:42:14 UTC
Permalink
Yes, my case seems to fall under the section "Class Template Argument
Deduction and partial template argument lists" in P1021. Lets hope it
passes!

BTW: It does work if I add a deduction guide with the exact same parameter
list as the constructor, in this case:

template<typename T, typename R> Bar(R r) -> Bar<T, R>;

This seems quite strange as we add the constructor we already had and
change the behaviour!
Post by Nicolas Lesser
See also P1021, "Filling holes in Class Template Argument Deduction".
https://wg21.link/p1021
Post by Bengt Gustafsson
My naive understanding was that a class template with constructors should
be callable as if it was a function, returning an object of an appropriate
type.
This works but excludes the situation where you in the function case
would have to provide a subset of template parameter values yourself. For
class templates you have to provide all or none.
template<typename T, typename R> void Foo(R r)
{
}
template<typename T, typename R> class Bar {
Bar(R r) {}
};
int main()
{
Foo<int>(3.14); // Works
Bar<int>(3.14); // Error
}
The Bar construction line does not compile on any of the main compilers,
which I think it should, just as Foo call does.
What is the good reason for not making this consistent?
(Godbolt: https://godbolt.org/z/b_L7bE)
--
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6083bb7d-fc99-459c-bfbc-045c15ec53b1%40isocpp.org.
Bengt Gustafsson
2018-11-02 13:55:41 UTC
Permalink
Yes, my case seems to fall under the section "Class Template Argument
Deduction and partial template argument lists" in P1021. Lets hope it
passes!
Post by Nicolas Lesser
See also P1021, "Filling holes in Class Template Argument Deduction".
https://wg21.link/p1021
Post by Bengt Gustafsson
My naive understanding was that a class template with constructors should
be callable as if it was a function, returning an object of an appropriate
type.
This works but excludes the situation where you in the function case
would have to provide a subset of template parameter values yourself. For
class templates you have to provide all or none.
template<typename T, typename R> void Foo(R r)
{
}
template<typename T, typename R> class Bar {
Bar(R r) {}
};
int main()
{
Foo<int>(3.14); // Works
Bar<int>(3.14); // Error
}
The Bar construction line does not compile on any of the main compilers,
which I think it should, just as Foo call does.
What is the good reason for not making this consistent?
(Godbolt: https://godbolt.org/z/b_L7bE)
--
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/945c77c8-18e4-40fe-8880-83346219bec4%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6824aaf9-11a6-4502-853c-60226d10c286%40isocpp.org.
Loading...