[C++11] Jak nadać inną nazwę dla istniejącego szablonu?
Ostatnio zmodyfikowano 2013-08-22 15:48
DejaVu Temat założony przez niniejszego użytkownika |
[C++11] Jak nadać inną nazwę dla istniejącego szablonu? » 2013-08-22 00:16:56 Rozwiązanie: #include <memory> #include <cstdio>
template < class T > using sptr = std::shared_ptr < T >;
int main() { sptr < int > wskaznik( new int ); * wskaznik = 5; printf( "Wartosc = %d\n", * wskaznik ); return 0; }
Nie działa pod Visual C++ 2012. Działa pod mingw32-g++ (tdm-1) 4.7.1 mingw32-g++.exe -std=c++11 -pthread -c przyklad.cpp -o przyklad.o mingw32-g++.exe -o przyklad.exe przyklad.o -static -static-libgcc
|
|
Mrovqa |
» 2013-08-22 15:48:07 Dodam także, iż dzięki tej metodzie można ustawić już niektóre parametry. W poniższym przykładzie "domniemuje" sobie typ drugiego parametru. #include <algorithm> #include <iostream>
template < class T, class U, class V > struct str { T t; U u; V v; };
template < class T, class U > using str2 = str < T, int, U >;
template < class T > void foo( T t ) { std::cout << t.t << " " << t.u << " " << t.v << "\n"; }
int main() { str < int, char, const char *> s1 = { 34, 'c', "string" }; str2 < float, char > s2 = { 6.9f, 13, 'P' }; foo( s1 ); foo( s2 ); return 0; }
Standardowe wyjście programu: 34 c string 6.9 13 P |
|
« 1 » |