Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOf result error if not match T #50

Open
lemon19900815 opened this issue Aug 10, 2023 · 0 comments
Open

IndexOf result error if not match T #50

lemon19900815 opened this issue Aug 10, 2023 · 0 comments

Comments

@lemon19900815
Copy link

template <typename T, typename... List>
struct IndexOf;

template <typename T, typename Head, typename... Rest>
struct IndexOf<T, Head, Rest...>
{
enum { value = IndexOf<T, Rest...>::value + 1 };
};

template <typename T, typename... Rest>
struct IndexOf<T, T, Rest...>
{
enum { value = 0 };
};

template
struct IndexOf
{
enum{value = -1};
};

template<typename T, typename...Args>
constexpr auto IndexOf_v = IndexOf<T, Args...>::value;

//error:如果不含T类型,返回结果是错误的
IndexOf_v<int, char, char*, double, int64> = 3

template<typename T, typename... Args>
constexpr auto IndexOf_v = std::conditional_t<Contains_v<T, Args...>,
IndexOf<T, Args...>, std::integral_constant<int32_t, -1>>::value;
我借助Contains模板首先查找是否有匹配的类型,然后再进行IndexOf模板操作来避免这个问题。不知道怎么来修改这个IndexOf模板来直接解决这个不含T类型时的错误问题。你能提供一些帮助吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant