2

新问题

编辑

这个错误似乎与这个相同

MSVC 拒绝以下代码:

template <template <typename T, typename T::mytype x> class X>
struct z
{};

template <typename T, typename T::mytype xx>
struct x
{};

z<x> zz;

错误 C3201:类模板“x”的模板参数列表与模板参数“X”的模板参数列表不匹配

但是类模板 x 具有与参数 X 相同的一组模板参数。

地狱!这个错误报告来自 2012 年 4 月?!

状态:延期关闭

:(

微软,我很失望TT

编辑的编辑

在我的情况下(如下),我可以用自己的方式做我想做的事,但这很丑:

//Library code
struct Boundary {
    double sup, inf;
};

template <
    class UserBoundaries, 
    template <size_t, bool...> class LocalInfo, 
    size_t hack>
struct Space_LocalParameters : LocalInfo<hack> {};


//User code
struct Boundaries_Problem7 {
    Boundary b1, b2, b3;
};

template <size_t, bool...>
struct LocalInfo_Problem7 {};

template <size_t hack, bool... args>
struct Problem7_Functions 
: Space_LocalParameters < Boundaries_Problem7, LocalInfo_Problem7, hack, args...> {};

int main() {}

利用:

//Compute the offset of member b2 in Boundaries_Problem7
const size_t hack = (size_t)(&((Boundaries_Problem7*)0)->b2);

if (...)
    problem.functions[k].SetFunction(&Problem7_Functions<hack, true, false>::f);
else if (...) 
    problem.functions[k].SetFunction(&Problem7_Functions<hack, false, true>::f);
else problem.functions[k].SetFunction(&Problem7_Functions<hack, false, false>::f);

//Later in that function
if (boundary_Inf) {
    value += this->points[0].coef 
             * ((Boundary*)((char*)&domain.boundaries + hack))->inf;
}
if (boundary_Sup) {
    value += this->points[N-1].coef 
             * ((Boundary*)((char*)&domain.boundaries + hack))->sup;
}

编译器是否允许在优化期间更改成员的偏移量?

编辑结束

新问题

以下在http://coliru.stacked-crooked.com/a/be088e06861f9349上编译得很好

//Library code
struct Boundary {};

template <
    class UserBoundaries, 
    template <Boundary UserBoundaries::*> class LocalInfo, 
    Boundary UserBoundaries::*m_ptr>
struct Space_LocalParameters : LocalInfo<m_ptr> {};


//User code
struct Boundaries_Problem7 {
    Boundary b1, b2, b3;
};

template <Boundary Boundaries_Problem7::*m_ptr>
struct LocalInfo_Problem7 {};

template <Boundary Boundaries_Problem7::*ptr>
struct Problem7_Functions 
: Space_LocalParameters < Boundaries_Problem7, LocalInfo_Problem7, ptr> {}; // <-- C3201


int main() {}

但是visual studio 2013给了我这个错误:

错误 C3201:类模板“LocalInfo_Problem7”的模板参数列表与模板参数“LocalInfo”的模板参数列表不匹配

这是VS编译器错误吗?还是我的代码错了?

我在这里写了一个错误报告https://connect.microsoft.com/VisualStudio/feedback/details/933984/c-visual-studio-2013-compiler-bug-template-template-pointer-to-member-issue

如果这是一个错误,我怎样才能让它工作?

谢谢你的帮助。

老问题

我在使用有点复杂的模板“typedef”时遇到了一些麻烦。

尽可能简化问题,我想出了这个:

template <class A, template <A*> class B, A* ptr>
struct Foo : B<ptr> {};

template <class, class AA, template <AA*> class BB, AA* ptr>
using Bar = Foo<AA, BB, ptr>;                           //  <-- C3201

Visual Studio 2013 给我:错误 C3201:类模板“BB”的模板参数列表与模板参数“B”的模板参数列表不匹配

我看不出有什么问题。

Foo<AA, BB, ptr>“扩展到”template <class AA, template <AA*> class BB, AA* ptr> struct Foo : BB<ptr> {}

理论上,如果我们定义以下内容(我实际上并没有尝试编译以下内容):

class MyClass {} myClass_instance;
template <MyClass*> class MyTemplateClass {};

然后

Bar<whatever, MyClass, MyTemplateClass, &myClass_instance> 

应该是别名

Foo<MyClass, MyTemplateClass, &myClass_instance> : MyTemplateClass<&myClass_instance>

这对我来说看起来不错。

奇怪的是,以下两个变体编译得很好:

1 /改变template <A*>template <AA*>template <Z*>

class Z;

template <class A, template <Z*> class B, A* ptr>
struct Foo : B<ptr> {};

template <class, class AA, template <Z*> class BB, AA* ptr>
using Bar = Foo<AA, BB, ptr>;                          

这种解决方法让我的程序运行良好,但这违背了我的模板方法的目的。

或者 2/ 去掉 Bar 的第一个模板参数

template <class A, template <A*> class B, A* ptr>
struct Foo : B<ptr> {};

template <class AA, template <AA*> class BB, AA* ptr>
using Bar = Foo<AA, BB, ptr>;                        

那么编译器提到的不匹配是什么?

删除 Bar 的第一个模板参数如何解决问题?

谢谢你的帮助。

4

0 回答 0