Fatal error with alias pattern

Note. Scroll to the bottom of the question to update.

When compiling my C ++ program with Visual Studio 2017 (with /std:c++latest

), I get the error:

'thrust :: tuple_element': template parameter 'N': 'i': variable with non-static storage duration cannot be used as non-type argument

The part of the code causing the error is as follows:

namespace thrust
{
template<size_t i, class... Types>
using tuple_element_t = typename tuple_element<i,Types...>::type;

      

The error is created specifically for the line starting with using

. The link Visual Studio provides for explaining the error ( https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2971 ) is pretty simple explaining that the local variable cannot be used as a template argument. However, I don't understand how this error applies in this case - the parameter i

was declared as shown in the above code as size_t i

in what I understand is the usual way of defining templates.

Also, after generating this error, Visual Studio creates a small popup that reads

Microsoft (R) C / C ++ compiler optimized performance

Windows is checking the solution to the problem ...

and then in the error list, the second error reads "could not recover previous error (s) by stopping compilation". This fatal error and the above error are the only two errors.

Why does the compiler generate the first error and how to fix it? And why would this error cause the compiler to crash?

By the way, tuple_element

which appears in the code causing errors from the Thrust parallel algorithms library (see https://thrust.github.io/doc/structthrust_1_1tuple__element.html ).

Note: use tuple_element_t

is as shown below.

namespace tuple_repeat_ns
{
template<typename T, typename IndexSequence>
struct tuple_repeat_helper;

template<typename T, size_t... I>
struct tuple_repeat_helper<T, std::index_sequence<I...>>
{
    using type = tuple<tuple_element_t<I-I,tuple<T>>...>;
};

template<typename T, size_t N>
struct tuple_repeat_traits : tuple_repeat_helper<T,std::make_index_sequence<N>>
{
    using typename tuple_repeat_helper<T,std::make_index_sequence<N>>::type;
};

template<size_t I, typename T>
auto tuple_repeat_identity(const T& t)
{
    return t;
}

template<typename T, size_t... I>
auto tuple_repeat(const T& t, std::index_sequence<I...>)
{
    return make_tuple(tuple_repeat_identity<I>(t)...);
}
}

template<typename T, size_t N>
using tuple_repeat_t = typename tuple_repeat_ns::tuple_repeat_traits<T, N>::type;

template<size_t N, typename T>
auto tuple_repeat(const T& t)
{
    return tuple_repeat_ns::tuple_repeat(t, std::make_index_sequence<N>{});
}

template<size_t N, typename T>
struct computes_tuple_repeat
{
    auto operator()(const T& t) const
    {
        return tuple_repeat<N,T>(t);
    }
};

      

Also, here are some of the actual compiler outputs:

1> [my_project] \ core \ thrust_wrapper.h (71): error C2971: 'thrust :: tuple_element': template parameter 'N': 'i': variable with non-static storage duration cannot be used as non-type argument
1> c: \ program files \ nvidia gpu computing toolkit \ cuda \ v8.0 \ include \ thrust \ tuple.h (67): note: see declaration "thrust :: tuple_element"
1> [my_project] \ core \ tuple_repeat.h (9): note: see declaration 'i'
1> [my_project] \ core \ tuple_repeat.h (9): note: see reference to the "tuple_element_t>" alias template instance compiled
    with
    [
        T = unsigned char
    ]
1> [my_project] \ core \ tuple_repeat.h (14): note: see the template instance reference 'tuple_repeat_ns ::tuple_repeat_helper> 'compiled
    with
    [
        T = unsigned char,
        _Ty = :: size_t
    ]
1> [my_project] \ core \ tuple_repeat.h (32): note: see the reference to the 'tuple_repeat_ns :: tuple_repeat_traits' class template instance compiled
    with
    [
        T = unsigned char
    ]
1> [my_project] \ core \ types.h (39): note: see the reference to the "tuple_repeat_t" alias template instance compiled
1> [my_project] \ core \ thrust_wrapper.h (71): fatal error C1903: Cannot Recover Previous Errors; stop compilation

Thanks for the help!

Update: it seems like replacing the line in tuple_repeat_helper

that reads

using type = tuple<tuple_element_t<I-I, tuple<T>>...>;

      

with line

using type = tuple<typename thrust::tuple_element<I-I,tuple<T>>::type...>;

      

prevents compiler error. This seems to solve the question of how to fix the problem, but why the error occurred from the start is still a mystery to me. I essentially just replaced the alias definition instead of just using the alias ... and who fixed it?

I'll probably try to put together a more suitable tested example later, and maybe post a bug report.

+3


source to share





All Articles