英文:
Passing designated initializers for a template class to a template function
问题
CTAD在最后一种情况下失败是因为编译器无法推断出模板类型B。有什么办法可以让它成功吗?
https://godbolt.org/z/7Es4Y11Yf
英文:
My goal is to create an API interface that looks like that this:
struct myAB{ int a,b; };
void function(myAB ab) {}
...
function({.a = 1, .b = 3});
The above works just fine. But if I want struct AB to have a templated type, CTAD fails.
template <class B>
struct myAB2{ int a; B b; };
template<typename B> myAB2(int, B) -> myAB2<B>;
template<typename B>
void function2(myAB2<B> ab) {}
...
myAB2 ab = {.a = 1, .b = 3}; //works just fine with CTAD
function2(ab); //fine as expected
function2(myAB2{.a = 1, .b = 3}); //works just fine with CTAD
function2({.a = 1, .b = 3}); //fails to compile, can't deduce type 'B'
Why does CTAD fail in the last case? Is there anything I can do to get it to succeed?
答案1
得分: 3
一个花括号初始化列表,包括其中包含的指定初始化器,没有类型。你只能从中推断出std::initializer_list
或数组类型,但在这里并没有发生。
由于{.a = 1, .b = 3}
不是一个表达式,也没有类型,所以从中无法推断出B
的任何信息,因此无法调用该函数。
function2<int>({.a = 1, .b = 3})
会起作用(因为myAB2<int> ab
可以用指定的初始化器进行初始化,不需要进行更多的推断)。
function2(myAB2{.a = 1, .b = 3})
也会起作用(因为它进行了CTAD以获得类型为myAB2<int>
的prvalue,从中可以推断出B = int
)。
英文:
A braced-init-list, including one that contains designated initializers, doesn't have a type. You can only ever deduce std::initializer_list
or array types from them, which isn't happening here.
Since {.a = 1, .b = 3}
is not an expression and doesn't have a type, function2({.a = 1, .b = 3})
cannot deduce anything for B
from it, so the function can't be called.
function2<int>({.a = 1, .b = 3})
would work (because the myAB2<int> ab
can be initialized with the designated initializers, no more deduction necessary).
So would function2(myAB2{.a = 1, .b = 3})
(because that does CTAD to get a prvalue of type myAB2<int>
, which B = int
can be deduced from).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论