Template argument deduction for class templates in C++17: am I doing it wrong?

前端 未结 1 1644
轻奢々
轻奢々 2021-01-13 03:10

According to https://gcc.gnu.org/projects/cxx-status.html, version 7 of g++, used with flag -std=c++1z, supports template argument deduction for class templates

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 03:58

    Class template argument deduction works for declaring instances of class types:

    Derived d(42);
    

    Or new-expressions:

    auto p = new Derived(42);
    

    Or function-style casts:

    foo(Derived(42));
    

    It does not work for declaring pointers.


    You'll have to simply provide the template arguments as you've always had to. Or, I guess:

    template  Base* downcast(Base* p) { return p; }
    auto pt_base = downcast(new Derived(42));
    

    0 讨论(0)
提交回复
热议问题