当我取消注释下面main()中的行时,Visual Studio 2015不会编译(否则代码会编译)。
#include <iostream>
#include <type_traits>
template <typename Output, std::size_t... Input> struct RemoveLastHelper;
template <template <std::size_t...> class Z, std::size_t... Accumulated, std::size_t First, std::size_t... Rest>
struct RemoveLastHelper<Z<Accumulated...>, First, Rest...> :
RemoveLastHelper<Z<Accumulated..., First>, Rest...> {};
template <template <std::size_t...> class Z, std::size_t... Accumulated, std::size_t Last>
struct RemoveLastHelper<Z<Accumulated...>, Last> {
using type = Z<Accumulated...>;
};
template <template <std::size_t...> class Z, std::size_t... Is>
using RemoveLast = RemoveLastHelper<Z<>, Is...>;
struct Base {
virtual ~Base() = default;
virtual void foo() const = 0;
};
template <std::size_t... Is> struct Derived;
template <std::size_t R>
struct Derived<R> : public Base {
virtual void foo() const override {}
};
// For example, Derived<R,D1,D2,D3> inherits from Derived<R,D1,D2>, which inherits from
// Derived<R,D1>, which inherits from Derived<R>, which inherits from Base (by the above).
template <std::size_t R, std::size_t... Is>
struct Derived<R, Is...> : public RemoveLast<Derived, R, Is...>::type {
virtual void foo() const override {RemoveLast<Derived, R, Is...>::type::foo();}
};
int main() {
// Derived<0,2,1> r; // This line won't compile.
}
它给出了错误:
'Derived<0,2>': invalid template argument for template parameter 'Z', expected a class template
'RemoveLast': left of '::' must be a class/struct/union
'type': is not a class or namespace name
那么这里有什么问题? GCC 5.1编译它,但我想知道这是否只是一个侥幸。语法virtual void foo() const override {RemoveLast<Derived, R, Is...>::type::foo();}
合法吗?如果没有,我该如何实现它(我只想让它调用它的基类&#39; foo())。如果它是合法的,我该如何重写它以便Visual Studio接受它(我需要它在Visual Studio上工作)?
答案 0 :(得分:5)
这是一个简单的解决方法。将Derived
替换为Derived::template Derived
。
template <std::size_t R, std::size_t... Is>
struct Derived<R, Is...> : public RemoveLast<Derived, R, Is...>::type {
virtual void foo() const override {RemoveLast<Derived::template Derived, R, Is...>::type::foo();}
};
::Derived
也有效。这似乎是Clang和Visual Studio中关于注入类名称的错误。