加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么在这种情况下我不需要模板参数?

发布时间:2020-12-16 03:21:37 所属栏目:百科 来源:网络整理
导读:我有这个代码: struct Base {};templatetypename Tstruct Foo : Base {};struct Bar { templatetypename T // v--- What's happening here? Bar(T foo) : baz{std::make_uniqueFoo(foo)} {} std::unique_ptrBase baz;}; 令人惊讶的是,GCC和Clang接受并编辑.
我有这个代码:
struct Base {};

template<typename T>
struct Foo : Base {};

struct Bar {
    template<typename T> //           v--- What's happening here?
    Bar(T foo) : baz{std::make_unique<Foo>(foo)} {}

    std::unique_ptr<Base> baz;
};

令人惊讶的是,GCC和Clang接受并编辑.它似乎是推导出Foo的模板参数,但是没有意义.编译器怎么接受,即使没有std :: make_unique的重载需要一个模板模板参数呢? Live example

解决方法

在某些情况下,模板总是无效的,无论提供什么样的模板参数,但编译器无法弄清楚,因为它无法尝试替换每个可能的模板参数集.根据标准([temp.res] / 8):

If no valid specialization can
be generated for a template,and that template is not instantiated,the template is ill-formed,no diagnostic
required.

这意味着编译器被允许是聪明的,并且证明没有有效的专业化,并且产生编译错误,但它也被允许不够聪明,而不会产生编译错误.当然,一旦模板被实例化,编译器就必须诊断错误.

使用没有模板参数的模板的名称是不合法的.在某些情况下,编译器可以推断参数.例如:

template <class T>
void foo(T x);

int main() {
    void (*p)(int) = foo;  // p points to foo<int>
}

在你的代码中,原来你已经在不能推导出模板参数的上下文中使用过Foo.如果编译器更聪明,那么他们就会想出来.但是他们没有想出来的事实并不意味着你的代码是正确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读