- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
#include <iostream>
template <typename T>
struct Symbol {};
template <>
struct Symbol<int> {
static constexpr const char value = 'd';
};
template <>
struct Symbol<float> {
static constexpr const char value = 'f';
};
template<std::size_t N, typename T>
constexpr bool check_arg_part(const char (&s)[N], size_t i, T d)
{
if (i == N)
return true;
if (i < N - 1) {
if (s[i] == '%') {
if (s[i + 1] != Symbol<T>::value)
return false;
}
}
return check_arg_part(s, i + 1, d);
}
template<std::size_t N, typename T>
constexpr bool check_arg(const char (&s)[N], T d) {
return check_arg_part(s, 0, d);
}
int main(int , char*[]) {
std::boolalpha(std::cout);
constexpr bool r = check_arg("foo is int: %d", 1);
std::cout << "Argument integer is correct: " << r << std::endl;
constexpr bool r1 = check_arg("foo is float: %f", 1.0f);
std::cout << "Argument float is correct: " << r1 << std::endl;
constexpr bool r2 = check_arg("foo is float: %f", 1);
std::cout << "Argument int is correct: " << r2 << std::endl;
return 0;
}