首页 > 解决方案 > C++17 typealias removes the ability of class template argument deduction

问题描述

The C++17 class template argument deduction works just fine, but I am not able to create a type alias on such a type, where the class template argument deduction works.

Are there any special tweaks to using needed, that it works?

Here I have a minimal working example:

#include <utility> 

template <typename T>
class cls {
   public:
    cls(T&& arg) : val(std::forward<T>(arg)) {}
   private:
    T val;
};

template <typename T>
using alias = cls<T>;

int main() {
    cls c(1);
    //alias b(2); //not working, but why?
    alias<int> a(2);
}

This code should compile with g++8 and --std=c++17, but if I uncomment the commented line, I get the following error:

test.cpp:16:11: error: missing template arguments before ‘b’
     alias b(2);//not working

Live example

标签: c++c++17

解决方案


推荐阅读