【发布时间】:2022-12-27 22:00:25
【问题描述】:
Here is a simple example of what I want, but I don't know how to make the functions unambiguous. I am using echo to check the integrity of the type of the member variable for SFINAE and echo back void as the return type.
// Example program
#include <iostream>
#include <string>
#include <type_traits>
namespace detail {
struct X {
int x;
};
struct Y {
int y;
};
template <typename V, typename... T>
using echo = V;
template <typename T>
echo<void, decltype(std::declval<T>().x)> template_print(T& t) {
std::cout << "x = " << t.x << std::endl;
}
template <typename T>
echo<void, decltype(std::declval<T>().y)> template_print(T& t) {
std::cout << "y = " << t.y << std::endl;
}
}
int main()
{
detail::X a{.x = 1};
detail::Y b{.y = 2};
detail::template_print(a);
detail::template_print(b);
return 0;
}
【问题讨论】:
标签: c++ template-meta-programming