Created
March 19, 2014 01:30
-
-
Save minamiyama1994/9633794 to your computer and use it in GitHub Desktop.
型安全なprintf -- 如何にしてコンパイル時に型エラーを検出するか -- ref: http://qiita.com/minamiyama1994/items/95ef490043c495be8c44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"TSP/TSP.hpp" | |
auto main ( ) -> int | |
{ | |
TSP_PRINTF ( "%c\n" , 'a' ) ; | |
TSP_PRINTF ( "%s\n" , "hoge" ) ; | |
TSP_PRINTF ( "%d\n" , 1 ) ; | |
TSP_PRINTF ( "%f\n" , 0.1 ) ; | |
TSP_PRINTF ( "%%\n%c\n%s\n%d\n%f\nHello! TSP!\n" , 'e' , "piyo" , 1 , 0.25 ) ; | |
// TSP_PRINTF ( "%f\n" , "hoge" ) ; // <- Compile Error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | |
hoge | |
1 | |
0.1 | |
% | |
e | |
piyo | |
1 | |
0.25 | |
Hello! TSP! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"TSP/TSP.hpp" | |
auto main ( ) -> int | |
{ | |
TSP_PRINTF ( "%c\n" , 'a' ) ; | |
TSP_PRINTF ( "%s\n" , "hoge" ) ; | |
TSP_PRINTF ( "%d\n" , 1 ) ; | |
TSP_PRINTF ( "%f\n" , 0.1 ) ; | |
TSP_PRINTF ( "%%\n%c\n%s\n%d\n%f\nHello! TSP!\n" , 'e' , "piyo" , 1 , 0.25 ) ; | |
TSP_PRINTF ( "%f\n" , "hoge" ) ; // <- Compile Error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In file included from example.cpp:1:0: | |
./include/TSP/TSP.hpp: In instantiation of 'constexpr auto tsp::printf(const T& ...) [with format_type = ftmp::list<ftmp::integral<char, '%'>, ftmp::integral<char, 'f'>, ftmp::integral<char, '\012'> >; T = {char [5]}]': | |
example.cpp:9:2: required from here | |
./include/TSP/TSP.hpp:255:55: error: no matching function for call to 'tsp::detail::printf<ftmp::list<ftmp::integral<char, '%'>, ftmp::integral<char,'f'>, ftmp::integral<char, '\012'> > >::func(const char [5])' | |
return detail::printf < format_type >::func ( v ... ) ; | |
^ | |
./include/TSP/TSP.hpp:255:55: note: candidate is: | |
./include/TSP/TSP.hpp:246:26: note: template<class ... T> static constexpr auto tsp::detail::printf_impl_format<ftmp::list<ftmp::integral<char, '%'>,ftmp::integral<char, 'f'>, ftmp::integral<char, ch>...> >::func(double, const T& ...) [with T = {T ...}; char ...ch = {'\012'}] | |
constexpr static auto func ( double h , const T & ... v ) | |
^ | |
./include/TSP/TSP.hpp:246:26: note: template argument deduction/substitution failed: | |
./include/TSP/TSP.hpp:255:55: note: cannot convert 'v#0' (type 'const char [5]') to type 'double' | |
return detail::printf < format_type >::func ( v ... ) ; | |
^ | |
example.cpp: In function 'int main()': | |
./include/TSP/TSP.hpp:261:72: error: void value not ignored as it ought to be | |
tsp::printf < SPROUT_PP_CAT ( format_type , __LINE__ )> ( __VA_ARGS__ ) ( ) | |
^ | |
example.cpp:9:2: note: in expansion of macro 'TSP_PRINTF' | |
TSP_PRINTF ( "%f\n" , "hoge" ) ; // <- Compile Error | |
^ | |
make: *** [all] Error 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment