fork download
  1. #include <iostream>
  2. #include <tuple>
  3. template<typename T> struct FunctionSignatureParser; // Must declare a primary template with a missing implementation
  4. template<typename Result, typename...Args> struct FunctionSignatureParser<Result(Args...)>
  5. {
  6. using return_type = Result;
  7. using args_tuple = std::tuple<Args...>;
  8. template <size_t i> struct arg
  9. {
  10. typedef typename std::tuple_element<i, args_tuple>::type type;
  11. };
  12. };
  13. short square(char x) { // 8-bit integer as input, 16-bit integer as output
  14. return short(x)*short(x);
  15. }
  16. int main() {
  17. char answer = 42;
  18. static_assert(std::is_same<char, FunctionSignatureParser<decltype(square)>::arg<0>::type>::value, "Function 'square' does not use an argument of type 'char'");
  19. static_assert(std::is_same<short, FunctionSignatureParser<decltype(square)>::return_type>::value, "Function 'square' does not return a value of type 'short'");
  20. short sqrAnswer = square(answer);
  21. std::cout << "The square of " << +answer << " is " << +sqrAnswer << std::endl;
  22. return 0;
  23. }
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
The square of 42 is 1764