为什么我会出现编译错误“函数参数'field'的未知值不能用于常量表达式”?
都标记为constexpr,我看不出在编译时知道值有什么问题。
有什么方法可以修复这个错误吗?
#include <tuple>
#include <string_view>
namespace {
template<typename Tuple, typename F, std::size_t... Indices>
constexpr void for_each_impl(Tuple &&tuple, F &&f, std::index_sequence<Indices...>) {
(f(std::get<Indices>(std::forward<Tuple>(tuple))), ...);
}
template<typename Tuple, typename F>
constexpr void for_each(Tuple &&tuple, F &&f) {
const auto N = std::tuple_size<std::remove_reference_t<Tuple>>::value;
for_each_impl(std::forward<Tuple>(tuple), std::forward<F>(f), std::make_index_sequence<N>{});
}
template <typename T, typename... Tuple>
constexpr auto has_type(const std::tuple<Tuple...> &tuple) {
return std::disjunction_v<std::is_same<T, Tuple>...>;
}
}// namespace
template<typename A>
struct meta_field {
constexpr meta_field(std::string_view name, A attributes)
: name(name), attributes(attributes) {
}
const std::string_view name;
const A attributes;
};
int main() {
constexpr auto fields = std::make_tuple(meta_field("a121213", std::make_tuple(int(5))), meta_field("hello", std::make_tuple()));
for_each(fields, [](const auto &field) {
// why unknown value?
if constexpr (has_type<int>(field.attributes)) {
}
});
}
Godbold链接
attributes
不是constexpr
。
您可以使它成为一个模板参数,并使它成为类中的static constexpr
。 但是,如果非类型模板参数是浮点值,则需要C++20。
template<auto... Attributes>
struct meta_field {
constexpr meta_field(std::string_view name)
: name(name) {
}
static constexpr std::tuple<decltype(Attributes)...> attributes{Attributes...};
const std::string_view name;
};
// ...
constexpr auto fields = std::make_tuple(
meta_field<5>("a121213"),
meta_field<>("hello"));
函数参数不是constexpr,因此您必须使用类型:
template <typename T, typename Tuple> struct has_type : std::false_type {};
template <typename T, typename... Ts> struct has_type<T, std::tuple<Ts...>> : std::disjunction<std::is_same<T, Ts>...> {};
用法类似于
for_each(fields, [](const auto &field) {
if constexpr (has_type<int, std::decay_t<decltype(field.attributes)>>::value) {
std::cout << std::get<int>(field.attributes) << std::endl;
}
});
演示