Search code examples
c++visual-studio-2017function-parametertemplate-variablesinternal-compiler-error

Working Around a Visual Studio Internal Compiler Error With Nested Templated Variables


I'm trying to write code that will let me index into the parameter types of a function:

template <typename R, typename... ARGS>
R function_return(R(*)(ARGS...));

template <typename R, typename... ARGS>
std::tuple<ARGS...> function_parameters(R(*)(ARGS...));

template <int I, typename T>
using get_type = typename std::conditional_t<(I < 0), std::tuple_element<static_cast<int>(std::tuple_size_v<T>) + I, T>, std::tuple_element<I, T>>::type;

template <int I, typename T>
using parameter_type = get_type<I, decltype(function_parameters(std::declval<T>()))>;

Live Example (ICE under VS) Live Example (working on GCC)

But when I try to use this on I get an internal compiler error:

fatal error C1001: An internal error has occurred in the compiler.

Is there another way that I can do this which might work around the internal compiler error?


Solution

  • As mentioned here it seems like struggles with templated using statements being passed to each other. (I'm seeing the internal compiler error on 15.6.7; as mentioned here this may have been fixed by patches.)

    I've been able to work around it by capturing all the functionality in a single using statement:

    template <typename R, typename... ARGS>
    R function_return(R(*)(ARGS...));
    
    template <typename R, typename... ARGS>
    std::tuple<ARGS...> function_parameters(R(*)(ARGS...));
    
    template <int I, typename T, typename X = decltype(function_parameters(std::declval<T>()))>
    using parameter_type = typename std::conditional_t<(I < 0), std::tuple_element<static_cast<int>(std::tuple_size_v<X>) + I, X>, std::tuple_element<I, X>>::type;