- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在子方法中扩展类的可变参数模板类型列表:
template<typename... P>
struct Foo
{
template<P...> // error C3522: 'P' : parameter
// pack cannot be expanded in this context
static void Bar(P... a){}
};
这段代码有什么问题,或者它只是一个 MSVS '12: Nov. '12 CTP 错误?
(是的,我知道这个例子中的显式模板特化是多余的。)
以上是我重现错误的最简单的情况。完整代码为:
template<typename FuncSignature>
class Callback;
template<typename R, typename... P>
class Callback<R (P...)>
{
public:
Callback() : func(0), obj(0) {}
Callback& operator=(const Callback& rhs)
{ obj = rhs.obj; func = rhs.func; return *this; }
private:
typedef R (*FuncType)(const void*, P...);
Callback(FuncType f, const void* o) : func(f), obj(o) {}
private:
FuncType func;
const void* obj;
template<typename FR, typename... FP>
friend class FreeCallbackFactory;
};
template<typename R, typename... P>
class FreeCallbackFactory
{
private:
template<R (*Func)(P...)>
static R Wrapper(const void*, P... a)
{
return (*Func)(a...);
}
public:
template<R (*Func)(P...)>
inline static Callback<R (P...)> Bind()
{
return Callback<R (P...)>
(&FreeCallbackFactory::Wrapper<Func>, 0);
}
};
template<typename R, typename... P>
inline FreeCallbackFactory<R, P...>
GetCallbackFactory(R (*)(P...))
{
return FreeCallbackFactory<R, P...>();
}
void Test(){}
int main(int argc, char** argv){
Callback<void ()> cb = GetCallbackFactory(&Test).Bind<&Test>()
}
它在 g++ 中编译得很好,所以我假设只是一个编译器错误,并且持续的发现仍然只指向这一点,除了显式地逐一扩展它们之外,是否有任何可能的解决方法?
最佳答案
代码看起来是正确的,但我怀疑它是否按照您的预期执行:声明
template <P...>
static void Bar(P... a);
声明一个将 P...
值作为模板参数和函数参数的函数。也就是说,P
的元素必须是允许非类型参数(例如,整数、指针或引用)的类型,并且您需要在调用时提供它们各自的值作为模板参数功能。调用这样的函数看起来像这样(尽管 gcc 和 clang 似乎都不需要传递模板参数):
Foo<int, int>::Bar<1, 2>(3, 4);
也就是说,根据 gcc 和 clang 生成的错误消息,它们似乎不会让您创建成员函数模板的特化,但我尚未在标准中对此进行验证。我认为,您可能应该省略模板声明并使用
static void Bar(P... a);
关于c++ - 在可变模板类中使用可变参数的显式特化 [MSVS '12: Nov. ' 12 CTP : error C3522],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13674311/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!