将模板类的指定初始化程序传递给模板函数。

huangapple go评论70阅读模式
英文:

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?

https://godbolt.org/z/7Es4Y11Yf

答案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&lt;int&gt;({.a = 1, .b = 3}) would work (because the myAB2&lt;int&gt; 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&lt;int&gt;, which B = int can be deduced from).

huangapple
  • 本文由 发表于 2023年7月11日 03:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定