且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有相同参数的不同模板模板参数的错误重载函数

更新时间:2023-02-11 11:06:01

别无选择,必须使用VC2010:(

Don't have a choice, must use VC2010 :(

如果是这样:知道如何解决吗?

If so: Any idea how to work around it?

所以,您可以尝试

template<template<typename> class TemplArgA, template<typename> class TemplArgB>
class CompileError {
public:
void func(TemplArgA<int> x) {}
void func(TemplArgB<int> x,void* workaround = 0) {}

当然,这并不完全等同于您的原始代码(因为直到在TemplArgA == TemplArgB情况下函数实例化之前您都不会收到错误;我不知道这是否与您相关)

of course, this is not strictly equivalent to your original code though (because you'll not get an error until function instantation in the TemplArgA==TemplArgB case; I don't know if this is relevant for you or not)

但是在真实的代码中,我不仅有TemplArgA和TemplArgB,而且还有4个模板模板参数(比如说TemplArgA到TemplArgD)-我认为我当时不能应用该解决方法吗?

But in the real code, I don't have just TemplArgA and TemplArgB, but 4 template template parameters (say TemplArgA to TemplArgD) -- I don't think I can apply the workaround then?

您只需要说服编译器这些重载是不等效的:

you just need to persuade the compiler that those overloads are not equivalent:

template<int> struct workaround_t{};

void func(TemplArgA<int> x, workaround_t<0>* workaround = 0) {}
void func(TemplArgB<int> x, workaround_t<1>* workaround = 0) {}
void func(TemplArgC<int> x, workaround_t<2>* workaround = 0) {}
//...