#1
by m3908714035 » 5年前
以前別トピック「
scanf を簡略化したい」で質問させていただいた者です。コードを書いている中でscanfの簡略とは異なった点で疑問が生じましたので、新しくトピックを立てさせていただきます。
code:
コード:
struct in
{
template<typename typ> in& operator>>(typ& val) { std::cin >> val; return *this; }
template<> in& operator>>(char& val) { std::scanf(" %c", &val); return *this; }
template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
template<> in& operator>>(float& val) { std::scanf("%f", &val); return *this; }
template<> in& operator>>(double& val) { std::scanf("%lf", &val); return *this; }
};
struct out
{
template<typename typ> template<>out& operator<<(typ& val) { std::cout << val; return *this; }
template<> out& operator<<(char& val) { std::printf("%c", val); return *this; }
template<> out& operator<<(std::int_fast8_t& val) { std::printf("%hd", val); return *this; }
template<> out& operator<<(std::uint_fast8_t& val) { std::printf("%hu", val); return *this; }
template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
template<> out& operator<<(float& val) { std::printf("%f", val); return *this; }
template<> out& operator<<(double& val) { std::printf("%lf", val); return *this; }
};
前回のコードにprintfも加えて、scanfとprintfをどのクラスに対してもinとoutで入出力できるようにしたものです。実際に書いたコードはマクロを使用して簡略化させてあります。また、int等のクラスが実行環境によって変わってしまうことがあるということを知ったため、cstdintを用いてintXXX_tを形を使いました。このinとoutを含んだコードを実行すると以下のエラーが発生しました。
error: (
https://wandbox.org/ C++ gcc HEAD 9.0.0 20181227 (experimental))
コード:
prog.cc:9:11: error: explicit specialization in non-namespace scope 'struct in'
9 | template<> in& operator>>(char& val) { std::scanf(" %c", &val); return *this; }
| ^
prog.cc:10:11: error: explicit specialization in non-namespace scope 'struct in'
10 | template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
| ^
prog.cc:11:11: error: explicit specialization in non-namespace scope 'struct in'
11 | template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
| ^
prog.cc:12:11: error: explicit specialization in non-namespace scope 'struct in'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^
prog.cc:13:11: error: explicit specialization in non-namespace scope 'struct in'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^
prog.cc:14:11: error: explicit specialization in non-namespace scope 'struct in'
14 | template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
| ^
prog.cc:14:17: error: 'in& in::operator>>(int_fast32_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
14 | template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
| ^~~~~~~~
prog.cc:12:17: note: previous declaration 'in& in::operator>>(int_fast16_t&)'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^~~~~~~~
prog.cc:15:11: error: explicit specialization in non-namespace scope 'struct in'
15 | template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
| ^
prog.cc:15:17: error: 'in& in::operator>>(uint_fast32_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
15 | template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
| ^~~~~~~~
prog.cc:13:17: note: previous declaration 'in& in::operator>>(uint_fast16_t&)'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^~~~~~~~
prog.cc:16:11: error: explicit specialization in non-namespace scope 'struct in'
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ^
prog.cc:16:17: error: 'in& in::operator>>(int_fast64_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ^~~~~~~~
prog.cc:12:17: note: previous declaration 'in& in::operator>>(int_fast16_t&)'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^~~~~~~~
prog.cc:17:11: error: explicit specialization in non-namespace scope 'struct in'
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ^
prog.cc:17:17: error: 'in& in::operator>>(uint_fast64_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ^~~~~~~~
prog.cc:13:17: note: previous declaration 'in& in::operator>>(uint_fast16_t&)'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^~~~~~~~
prog.cc:18:11: error: explicit specialization in non-namespace scope 'struct in'
18 | template<> in& operator>>(float& val) { std::scanf("%f", &val); return *this; }
| ^
prog.cc:19:11: error: explicit specialization in non-namespace scope 'struct in'
19 | template<> in& operator>>(double& val) { std::scanf("%lf", &val); return *this; }
| ^
prog.cc: In member function 'in& in::operator>>(int_fast8_t&)':
prog.cc:10:67: warning: format '%hd' expects argument of type 'short int*', but argument 2 has type 'int_fast8_t*' {aka 'signed char*'} [-Wformat=]
10 | template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
| ~~^ ~~~~
| | |
| | int_fast8_t* {aka signed char*}
| short int*
| %hhd
prog.cc: In member function 'in& in::operator>>(uint_fast8_t&)':
prog.cc:11:68: warning: format '%hu' expects argument of type 'short unsigned int*', but argument 2 has type 'uint_fast8_t*' {aka 'unsigned char*'} [-Wformat=]
11 | template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
| ~~^ ~~~~
| | |
| | uint_fast8_t* {aka unsigned char*}
| short unsigned int*
| %hhu
prog.cc: In member function 'in& in::operator>>(int_fast16_t&)':
prog.cc:12:67: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'int_fast16_t*' {aka 'long int*'} [-Wformat=]
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ~^ ~~~~
| | |
| | int_fast16_t* {aka long int*}
| int*
| %ld
prog.cc: In member function 'in& in::operator>>(uint_fast16_t&)':
prog.cc:13:68: warning: format '%u' expects argument of type 'unsigned int*', but argument 2 has type 'uint_fast16_t*' {aka 'long unsigned int*'} [-Wformat=]
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ~^ ~~~~
| | |
| | uint_fast16_t* {aka long unsigned int*}
| unsigned int*
| %lu
prog.cc: In member function 'in& in::operator>>(int_fast64_t&)':
prog.cc:16:69: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int_fast64_t*' {aka 'long int*'} [-Wformat=]
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ~~~^ ~~~~
| | |
| | int_fast64_t* {aka long int*}
| long long int*
| %ld
prog.cc: In member function 'in& in::operator>>(uint_fast64_t&)':
prog.cc:17:70: warning: format '%llu' expects argument of type 'long long unsigned int*', but argument 2 has type 'uint_fast64_t*' {aka 'long unsigned int*'} [-Wformat=]
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ~~~^ ~~~~
| | |
| | uint_fast64_t* {aka long unsigned int*}
| long long unsigned int*
| %lu
prog.cc: At global scope:
prog.cc:24:37: error: invalid explicit specialization before '>' token
24 | template<typename typ> template<> out& operator<<(typ& val) { std::cout << val; return *this; }
| ^
prog.cc:24:37: error: explicit specialization in non-namespace scope 'struct out'
prog.cc:24:42: error: too many template-parameter-lists
24 | template<typename typ> template<> out& operator<<(typ& val) { std::cout << val; return *this; }
| ^
prog.cc:25:11: error: explicit specialization in non-namespace scope 'struct out'
25 | template<> out& operator<<(char& val) { std::printf("%c", val); return *this; }
| ^
prog.cc:26:11: error: explicit specialization in non-namespace scope 'struct out'
26 | template<> out& operator<<(std::int_fast8_t& val) { std::printf("%hd", val); return *this; }
| ^
prog.cc:27:11: error: explicit specialization in non-namespace scope 'struct out'
27 | template<> out& operator<<(std::uint_fast8_t& val) { std::printf("%hu", val); return *this; }
| ^
prog.cc:28:11: error: explicit specialization in non-namespace scope 'struct out'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^
prog.cc:29:11: error: explicit specialization in non-namespace scope 'struct out'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^
prog.cc:30:11: error: explicit specialization in non-namespace scope 'struct out'
30 | template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
| ^
prog.cc:30:18: error: 'out& out::operator<<(int_fast32_t&)' cannot be overloaded with 'out& out::operator<<(int_fast16_t&)'
30 | template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
| ^~~~~~~~
prog.cc:28:18: note: previous declaration 'out& out::operator<<(int_fast16_t&)'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^~~~~~~~
prog.cc:31:11: error: explicit specialization in non-namespace scope 'struct out'
31 | template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
| ^
prog.cc:31:18: error: 'out& out::operator<<(uint_fast32_t&)' cannot be overloaded with 'out& out::operator<<(uint_fast16_t&)'
31 | template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
| ^~~~~~~~
prog.cc:29:18: note: previous declaration 'out& out::operator<<(uint_fast16_t&)'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^~~~~~~~
prog.cc:32:11: error: explicit specialization in non-namespace scope 'struct out'
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ^
prog.cc:32:18: error: 'out& out::operator<<(int_fast64_t&)' cannot be overloaded with 'out& out::operator<<(int_fast16_t&)'
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ^~~~~~~~
prog.cc:28:18: note: previous declaration 'out& out::operator<<(int_fast16_t&)'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^~~~~~~~
prog.cc:33:11: error: explicit specialization in non-namespace scope 'struct out'
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ^
prog.cc:33:18: error: 'out& out::operator<<(uint_fast64_t&)' cannot be overloaded with 'out& out::operator<<(uint_fast16_t&)'
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ^~~~~~~~
prog.cc:29:18: note: previous declaration 'out& out::operator<<(uint_fast16_t&)'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^~~~~~~~
prog.cc:34:11: error: explicit specialization in non-namespace scope 'struct out'
34 | template<> out& operator<<(float& val) { std::printf("%f", val); return *this; }
| ^
prog.cc:35:11: error: explicit specialization in non-namespace scope 'struct out'
35 | template<> out& operator<<(double& val) { std::printf("%lf", val); return *this; }
| ^
prog.cc: In member function 'out& out::operator<<(int_fast16_t&)':
prog.cc:28:69: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int_fast16_t' {aka 'long int'} [-Wformat=]
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ~^ ~~~
| | |
| int int_fast16_t {aka long int}
| %ld
prog.cc: In member function 'out& out::operator<<(uint_fast16_t&)':
prog.cc:29:70: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint_fast16_t' {aka 'long unsigned int'} [-Wformat=]
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ~^ ~~~
| | |
| | uint_fast16_t {aka long unsigned int}
| unsigned int
| %lu
prog.cc: In member function 'out& out::operator<<(int_fast64_t&)':
prog.cc:32:71: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int_fast64_t' {aka 'long int'} [-Wformat=]
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ~~~^ ~~~
| | |
| | int_fast64_t {aka long int}
| long long int
| %ld
prog.cc: In member function 'out& out::operator<<(uint_fast64_t&)':
prog.cc:33:72: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint_fast64_t' {aka 'long unsigned int'} [-Wformat=]
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ~~~^ ~~~
| | |
| | uint_fast64_t {aka long unsigned int}
| long long unsigned int
| %lu
今回の疑問点は4つです。
①explicit specializationとは?
②どうしてクラス同士で衝突が起こり同時にオペレータを定義できないのか。
③-1 ex) int_fast64_tはcstdintでtypedef long long int_fast64_t;と定義されているのにエラーではint_fast64_t {aka long int}と型が異なっているのは何故か。
③-2 そもそもcstdintで以下のように3つとも同じように定義されているのに「_fastタイプが各プラットフォームがハードウェアに「便利」なタイプを指定できるタイプエイリアス」となるのはどうしてか。
cstdint(一部抜粋)
コード:
typedef int int32_t;
typedef int int_least32_t;
typedef int int_fast32_t;
自身でも様々なサイトを参考にエラーを訂正しているつもりですが、プログラミング経験がまだまだ浅く上手くいきません。もしかするとしょうもない部分を質問しているのかもしれませんが、改善策と共にご教示いただけると幸いです。
以前別トピック「[url=https://dixq.net/forum/viewtopic.php?f=3&t=20488]scanf を簡略化したい[/url]」で質問させていただいた者です。コードを書いている中でscanfの簡略とは異なった点で疑問が生じましたので、新しくトピックを立てさせていただきます。
code:
[code]
struct in
{
template<typename typ> in& operator>>(typ& val) { std::cin >> val; return *this; }
template<> in& operator>>(char& val) { std::scanf(" %c", &val); return *this; }
template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
template<> in& operator>>(float& val) { std::scanf("%f", &val); return *this; }
template<> in& operator>>(double& val) { std::scanf("%lf", &val); return *this; }
};
struct out
{
template<typename typ> template<>out& operator<<(typ& val) { std::cout << val; return *this; }
template<> out& operator<<(char& val) { std::printf("%c", val); return *this; }
template<> out& operator<<(std::int_fast8_t& val) { std::printf("%hd", val); return *this; }
template<> out& operator<<(std::uint_fast8_t& val) { std::printf("%hu", val); return *this; }
template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
template<> out& operator<<(float& val) { std::printf("%f", val); return *this; }
template<> out& operator<<(double& val) { std::printf("%lf", val); return *this; }
};
[/code]
前回のコードにprintfも加えて、scanfとprintfをどのクラスに対してもinとoutで入出力できるようにしたものです。実際に書いたコードはマクロを使用して簡略化させてあります。また、int等のクラスが実行環境によって変わってしまうことがあるということを知ったため、cstdintを用いてintXXX_tを形を使いました。このinとoutを含んだコードを実行すると以下のエラーが発生しました。
error: (https://wandbox.org/ C++ gcc HEAD 9.0.0 20181227 (experimental))
[code]
prog.cc:9:11: error: explicit specialization in non-namespace scope 'struct in'
9 | template<> in& operator>>(char& val) { std::scanf(" %c", &val); return *this; }
| ^
prog.cc:10:11: error: explicit specialization in non-namespace scope 'struct in'
10 | template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
| ^
prog.cc:11:11: error: explicit specialization in non-namespace scope 'struct in'
11 | template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
| ^
prog.cc:12:11: error: explicit specialization in non-namespace scope 'struct in'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^
prog.cc:13:11: error: explicit specialization in non-namespace scope 'struct in'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^
prog.cc:14:11: error: explicit specialization in non-namespace scope 'struct in'
14 | template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
| ^
prog.cc:14:17: error: 'in& in::operator>>(int_fast32_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
14 | template<> in& operator>>(std::int_fast32_t& val) { std::scanf("%ld", &val); return *this; }
| ^~~~~~~~
prog.cc:12:17: note: previous declaration 'in& in::operator>>(int_fast16_t&)'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^~~~~~~~
prog.cc:15:11: error: explicit specialization in non-namespace scope 'struct in'
15 | template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
| ^
prog.cc:15:17: error: 'in& in::operator>>(uint_fast32_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
15 | template<> in& operator>>(std::uint_fast32_t& val) { std::scanf("%lu", &val); return *this; }
| ^~~~~~~~
prog.cc:13:17: note: previous declaration 'in& in::operator>>(uint_fast16_t&)'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^~~~~~~~
prog.cc:16:11: error: explicit specialization in non-namespace scope 'struct in'
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ^
prog.cc:16:17: error: 'in& in::operator>>(int_fast64_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ^~~~~~~~
prog.cc:12:17: note: previous declaration 'in& in::operator>>(int_fast16_t&)'
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ^~~~~~~~
prog.cc:17:11: error: explicit specialization in non-namespace scope 'struct in'
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ^
prog.cc:17:17: error: 'in& in::operator>>(uint_fast64_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ^~~~~~~~
prog.cc:13:17: note: previous declaration 'in& in::operator>>(uint_fast16_t&)'
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ^~~~~~~~
prog.cc:18:11: error: explicit specialization in non-namespace scope 'struct in'
18 | template<> in& operator>>(float& val) { std::scanf("%f", &val); return *this; }
| ^
prog.cc:19:11: error: explicit specialization in non-namespace scope 'struct in'
19 | template<> in& operator>>(double& val) { std::scanf("%lf", &val); return *this; }
| ^
prog.cc: In member function 'in& in::operator>>(int_fast8_t&)':
prog.cc:10:67: warning: format '%hd' expects argument of type 'short int*', but argument 2 has type 'int_fast8_t*' {aka 'signed char*'} [-Wformat=]
10 | template<> in& operator>>(std::int_fast8_t& val) { std::scanf("%hd", &val); return *this; }
| ~~^ ~~~~
| | |
| | int_fast8_t* {aka signed char*}
| short int*
| %hhd
prog.cc: In member function 'in& in::operator>>(uint_fast8_t&)':
prog.cc:11:68: warning: format '%hu' expects argument of type 'short unsigned int*', but argument 2 has type 'uint_fast8_t*' {aka 'unsigned char*'} [-Wformat=]
11 | template<> in& operator>>(std::uint_fast8_t& val) { std::scanf("%hu", &val); return *this; }
| ~~^ ~~~~
| | |
| | uint_fast8_t* {aka unsigned char*}
| short unsigned int*
| %hhu
prog.cc: In member function 'in& in::operator>>(int_fast16_t&)':
prog.cc:12:67: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'int_fast16_t*' {aka 'long int*'} [-Wformat=]
12 | template<> in& operator>>(std::int_fast16_t& val) { std::scanf("%d", &val); return *this; }
| ~^ ~~~~
| | |
| | int_fast16_t* {aka long int*}
| int*
| %ld
prog.cc: In member function 'in& in::operator>>(uint_fast16_t&)':
prog.cc:13:68: warning: format '%u' expects argument of type 'unsigned int*', but argument 2 has type 'uint_fast16_t*' {aka 'long unsigned int*'} [-Wformat=]
13 | template<> in& operator>>(std::uint_fast16_t& val) { std::scanf("%u", &val); return *this; }
| ~^ ~~~~
| | |
| | uint_fast16_t* {aka long unsigned int*}
| unsigned int*
| %lu
prog.cc: In member function 'in& in::operator>>(int_fast64_t&)':
prog.cc:16:69: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int_fast64_t*' {aka 'long int*'} [-Wformat=]
16 | template<> in& operator>>(std::int_fast64_t& val) { std::scanf("%lld", &val); return *this; }
| ~~~^ ~~~~
| | |
| | int_fast64_t* {aka long int*}
| long long int*
| %ld
prog.cc: In member function 'in& in::operator>>(uint_fast64_t&)':
prog.cc:17:70: warning: format '%llu' expects argument of type 'long long unsigned int*', but argument 2 has type 'uint_fast64_t*' {aka 'long unsigned int*'} [-Wformat=]
17 | template<> in& operator>>(std::uint_fast64_t& val) { std::scanf("%llu", &val); return *this; }
| ~~~^ ~~~~
| | |
| | uint_fast64_t* {aka long unsigned int*}
| long long unsigned int*
| %lu
prog.cc: At global scope:
prog.cc:24:37: error: invalid explicit specialization before '>' token
24 | template<typename typ> template<> out& operator<<(typ& val) { std::cout << val; return *this; }
| ^
prog.cc:24:37: error: explicit specialization in non-namespace scope 'struct out'
prog.cc:24:42: error: too many template-parameter-lists
24 | template<typename typ> template<> out& operator<<(typ& val) { std::cout << val; return *this; }
| ^
prog.cc:25:11: error: explicit specialization in non-namespace scope 'struct out'
25 | template<> out& operator<<(char& val) { std::printf("%c", val); return *this; }
| ^
prog.cc:26:11: error: explicit specialization in non-namespace scope 'struct out'
26 | template<> out& operator<<(std::int_fast8_t& val) { std::printf("%hd", val); return *this; }
| ^
prog.cc:27:11: error: explicit specialization in non-namespace scope 'struct out'
27 | template<> out& operator<<(std::uint_fast8_t& val) { std::printf("%hu", val); return *this; }
| ^
prog.cc:28:11: error: explicit specialization in non-namespace scope 'struct out'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^
prog.cc:29:11: error: explicit specialization in non-namespace scope 'struct out'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^
prog.cc:30:11: error: explicit specialization in non-namespace scope 'struct out'
30 | template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
| ^
prog.cc:30:18: error: 'out& out::operator<<(int_fast32_t&)' cannot be overloaded with 'out& out::operator<<(int_fast16_t&)'
30 | template<> out& operator<<(std::int_fast32_t& val) { std::printf("%ld", val); return *this; }
| ^~~~~~~~
prog.cc:28:18: note: previous declaration 'out& out::operator<<(int_fast16_t&)'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^~~~~~~~
prog.cc:31:11: error: explicit specialization in non-namespace scope 'struct out'
31 | template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
| ^
prog.cc:31:18: error: 'out& out::operator<<(uint_fast32_t&)' cannot be overloaded with 'out& out::operator<<(uint_fast16_t&)'
31 | template<> out& operator<<(std::uint_fast32_t& val) { std::printf("%lu", val); return *this; }
| ^~~~~~~~
prog.cc:29:18: note: previous declaration 'out& out::operator<<(uint_fast16_t&)'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^~~~~~~~
prog.cc:32:11: error: explicit specialization in non-namespace scope 'struct out'
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ^
prog.cc:32:18: error: 'out& out::operator<<(int_fast64_t&)' cannot be overloaded with 'out& out::operator<<(int_fast16_t&)'
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ^~~~~~~~
prog.cc:28:18: note: previous declaration 'out& out::operator<<(int_fast16_t&)'
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ^~~~~~~~
prog.cc:33:11: error: explicit specialization in non-namespace scope 'struct out'
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ^
prog.cc:33:18: error: 'out& out::operator<<(uint_fast64_t&)' cannot be overloaded with 'out& out::operator<<(uint_fast16_t&)'
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ^~~~~~~~
prog.cc:29:18: note: previous declaration 'out& out::operator<<(uint_fast16_t&)'
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ^~~~~~~~
prog.cc:34:11: error: explicit specialization in non-namespace scope 'struct out'
34 | template<> out& operator<<(float& val) { std::printf("%f", val); return *this; }
| ^
prog.cc:35:11: error: explicit specialization in non-namespace scope 'struct out'
35 | template<> out& operator<<(double& val) { std::printf("%lf", val); return *this; }
| ^
prog.cc: In member function 'out& out::operator<<(int_fast16_t&)':
prog.cc:28:69: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int_fast16_t' {aka 'long int'} [-Wformat=]
28 | template<> out& operator<<(std::int_fast16_t& val) { std::printf("%d", val); return *this; }
| ~^ ~~~
| | |
| int int_fast16_t {aka long int}
| %ld
prog.cc: In member function 'out& out::operator<<(uint_fast16_t&)':
prog.cc:29:70: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint_fast16_t' {aka 'long unsigned int'} [-Wformat=]
29 | template<> out& operator<<(std::uint_fast16_t& val) { std::printf("%u", val); return *this; }
| ~^ ~~~
| | |
| | uint_fast16_t {aka long unsigned int}
| unsigned int
| %lu
prog.cc: In member function 'out& out::operator<<(int_fast64_t&)':
prog.cc:32:71: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int_fast64_t' {aka 'long int'} [-Wformat=]
32 | template<> out& operator<<(std::int_fast64_t& val) { std::printf("%lld", val); return *this; }
| ~~~^ ~~~
| | |
| | int_fast64_t {aka long int}
| long long int
| %ld
prog.cc: In member function 'out& out::operator<<(uint_fast64_t&)':
prog.cc:33:72: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint_fast64_t' {aka 'long unsigned int'} [-Wformat=]
33 | template<> out& operator<<(std::uint_fast64_t& val) { std::printf("%llu", val); return *this; }
| ~~~^ ~~~
| | |
| | uint_fast64_t {aka long unsigned int}
| long long unsigned int
| %lu
[/code]
今回の疑問点は4つです。
①explicit specializationとは?
②どうしてクラス同士で衝突が起こり同時にオペレータを定義できないのか。
③-1 ex) int_fast64_tはcstdintでtypedef long long int_fast64_t;と定義されているのにエラーではint_fast64_t {aka long int}と型が異なっているのは何故か。
③-2 そもそもcstdintで以下のように3つとも同じように定義されているのに「_fastタイプが各プラットフォームがハードウェアに「便利」なタイプを指定できるタイプエイリアス」となるのはどうしてか。
cstdint(一部抜粋)
[code]
typedef int int32_t;
typedef int int_least32_t;
typedef int int_fast32_t;
[/code]
自身でも様々なサイトを参考にエラーを訂正しているつもりですが、プログラミング経験がまだまだ浅く上手くいきません。もしかするとしょうもない部分を質問しているのかもしれませんが、改善策と共にご教示いただけると幸いです。