gpt4 book ai didi

C++ 模板类继承[无法转换模板]

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:43 25 4
gpt4 key购买 nike

我有以下代码:

#include <iostream>
using namespace std;

template<int k>
struct Base{
int a = k;
};

struct C1 : Base<1>{};
struct C2 : Base<2>{int b;};

typedef Base<1> C1T;


template<
typename BufferType,

typename VerticesType,
VerticesType BufferType::* VerticesField = nullptr
>
void t(){};


int main() {
// WHY this work??
t<C1T , int, &C1T::a>();

// And this not?
// t<C1 , int, &C1::a>();

// ok.
// t< Base<1>, int, &Base<1>::a >();

// also, ok
t<C2 , int, &C2::b>();
return 0;
}

http://ideone.com/8tWCJS

为什么我不能这样称呼“t”?

t<C1 , int, &C1::a>();

但是,我收到以下错误:

Could not convert template argument ‘&Base<1>::a’ to ‘int C1::*’

附言如果 C1 是类型定义的,我就能理解这种行为......

最佳答案

尝试:

t<Base<1> , int, &C1::a>();

关于C++ 模板类继承[无法转换模板],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20732416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com