How i can use "using" keyword in multiple inheritance when parameter pack is template parameter of base class?
Code below compiles just fine
struct A
{
void foo(int) {}
};
struct B
{
void foo(int) {}
};
template<typename ...Types>
struct C : public Types...
{
using Types::foo...;
};
int main()
{
C<A,B> c;
}
But if i use template instead of A and B - I've got compile error
template<typename T>
struct TA {};
template<>
struct TA<int>
{
void foo(int) {}
};
template<>
struct TA<double>
{
void foo(int) {}
};
template<typename ...Types>
struct TC : public TA<Types>...
{
using TA<Types>::foo...; // ERROR C3520
};
Error:
error C3520: 'Types': parameter pack must be expanded in this context
How to rewrite second piece of code to get it work?
PS I tried this code with gcc and it's compiles without errors. But now I am using msvc...
-
Visual Studio has a persisting problem with unpacking parameter packs in some circumstances.François Andrieux– François Andrieux2019年08月19日 17:22:48 +00:00Commented Aug 19, 2019 at 17:22
-
Related to variadic-base-class-using-declaration-fails-to-compile-in-msvcJarod42– Jarod422019年08月19日 18:24:37 +00:00Commented Aug 19, 2019 at 18:24
1 Answer 1
Since it's a known MSVC bug, if you still want to make this work with MSVC you'll have to do it the (not very) hard way:
template <typename ...Types>
struct TC;
template <typename T>
struct TC<T> : TA<T>
{
using TA<T>::foo;
};
template <typename T, typename ...Types>
struct TC<T, Types...> : public TC<T>, public TC<Types...>
{
using TC<T>::foo;
using TC<Types...>::foo;
};
answered Aug 19, 2019 at 17:43
r3mus n0x
6,1941 gold badge17 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
L. F.
In fact, this is how things have to be done prior to C++17
Explore related questions
See similar questions with these tags.
lang-cpp