如何使使用基类构造函数的派生类的参数演绎工作?

发布时间:2022-05-22 / 作者:清心寡欲
本文介绍了如何使使用基类构造函数的派生类的参数演绎工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当派生类使用基类构造函数时,演绎似乎总是fail。然而,当基类有许多构造函数时,重新定义所有构造函数是非常笨拙的。当基类使用新的构造函数快速演变时,这也是一件痛苦的事情。这个老问题是在两年多前提出的,所以我想知道:到2020年,当c++17和c++2a可用时,有什么办法可以解决这个问题吗?

template
class base_t
{
public:
    base_t(As... args){}
};

template
class A_t: public base_t
{
public:
    A_t(As... args): base_t{args...} {};
};

template
class B_t: public base_t
{
    using base_t::base_t;
};

int main()
{
    base_t a{1, 2.0f};
    A_t{1, 2.0f};
    B_t{1, 2.0f}; //fails unless explicitly specialize the template
    return 0;
}

根据@Sam和@Barry:

更新

演绎指南很有帮助。然而,对于稍微复杂的情况,它仍然失控:

template 
struct D_t {
    A x;
    D_t(A x) :x{x} {}
};
template
class base2_t
{
public:
    base2_t(A a, B b){std::cout << "1
";}
    base2_t(A a, D_t c, int x){std::cout << "2
";}
    base2_t(A a, B b, int x){std::cout << "3
";}
    base2_t(A a, B b, int x, float y){std::cout << "4
";}
    explicit base2_t(A(*fp)(B)){std::cout << "5
";}
    // if we have lots of similar things like above
    // we will quickly end up write lots of different
    // guides.
};
template
class C_t: public base2_t
{
    using base2_t::base2_t;
};
template
C_t(A, B, As...)->C_t;
template
C_t(A(*)(B))->C_t;
float func1(int x)
{
    return x;
}
int main()
{
    C_t{1, 2.0f, 3};
    base2_t{1, D_t{2.0f}, 3};
    C_t{1, D_t{2.0f}, 3}; // this is wrong, we have to deal with it by checking types and write different guides.
    base2_t{&func1};
    C_t{&func1};
}

推荐答案

能够从基类继承演绎指南是proposed在C++20中。但是,此功能没有实现,正如最后一行所说:

来自继承的构造函数的CTAD的措辞没有在C++20委员会草案中及时敲定,将在以后的某个时间点在单独的措辞文件中发布。

因此,从现在起,您将需要显式地为派生类提供演绎指南(或者像为A_t那样定义构造函数)。希望这个问题将在c++23中得到解决。

这篇关于如何使使用基类构造函数的派生类的参数演绎工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]how to make argument deduction work for derived class which using base class constructor?


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。