Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can `using` work without specifying template parameters? [duplicate]

Is it possible to use using with a template class without specifying its template parameters straight away?

#include <array>
    
using MyArray = std::array<int, 5>;
using MyArrayT = std::array;         // error: missing template arguments after 'std::array'
    
int main() {
    
    MyArray myArray1;
    MyArrayT<char, 10> myArray2;
}

P.S.: Without using preprocessor tricks...

like image 701
Pietro Avatar asked May 07 '26 12:05

Pietro


1 Answers

You cannot do it like that because a "normal" type alias (using statement) is aliasing a concrete type with a new name, and std::array is not a concrete type.

But you can make the type alias itself templated (syntax (2)):

#include <cstddef>  // for std::size_t
#include <array>

template <typename T, std::size_t N>
using MyArrayT = std::array<T, N>;            

Then use it like you attempted:

MyArrayT<char, 10> myArray2;

Live demo

like image 146
wohlstad Avatar answered May 10 '26 00:05

wohlstad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!