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

etl::nth_type not implemented correctly #530

Closed
bitwizeshift opened this issue Apr 7, 2022 · 2 comments
Closed

etl::nth_type not implemented correctly #530

bitwizeshift opened this issue Apr 7, 2022 · 2 comments
Assignees
Labels

Comments

@bitwizeshift
Copy link

bitwizeshift commented Apr 7, 2022

I was just browsing through some of the utilities in ETL, and I noticed that nth_type appears to be implemented incorrectly as of 6780a56f52e38d4d7c095165694905afa202aaa9.

nth_type_helper does not seem to check if it's at the Nth type -- it always falls through to the last type in the variadic set due to what I believe to be an incorrect partial-specialization:

template <size_t N, typename T1, typename... TRest>
struct nth_type_helper
{
using type = typename nth_type_helper<N - 1U, TRest...>::type;
};
template <size_t N, typename T1>
struct nth_type_helper<N, T1>
{
using type = T1;
};

The size_t N parameter is always decremented, but never checked for a specific value -- so once N = 0 it will just roll over to static_cast<size_t>(-1).

This can be tested with:

static_assert(std::is_same<etl::nth_type_t<0,int,long>, int>::value, "");
static_assert(std::is_same<etl::nth_type_t<1,int,long>, long>::value, "");

The first test fails because it yields long instead of int (Live Example)

From inspection, it appears that the partial specialization for nth_type_helper should actually be:

    template <typename T1, typename... TRest>
    struct nth_type_helper<0, T1, TRest...>

which I believe is the desired behavior (Live Example)


(I don't have time to clone/make the change/open a PR, but this was quick enough to type up, and should be easy enough to fix)

@jwellbelove
Copy link
Contributor

Thanks 👍

@jwellbelove jwellbelove self-assigned this Apr 8, 2022
@jwellbelove jwellbelove added the bug label Apr 8, 2022
@jwellbelove
Copy link
Contributor

Fixed 20.27.2

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

No branches or pull requests

2 participants