使用未定义的模板类型(use of undefined template type)

编程入门 行业动态 更新时间:2024-10-26 02:24:21
使用未定义的模板类型(use of undefined template type)

在以下代码中

template<unsigned bits_count, typename ut_t, typename udt_t, template <typename> class meta_t> struct uint_t { typedef uint_t<bits_count, ut_t, udt_t, meta_t> t; typedef ut_t ut; typedef udt_t udt; typedef meta_t<t> meta; ut comp[meta::array_count]; }; namespace mxi { template<typename type> struct basic_info { static const bool is_native = true; static const bool is_signed = (type(-1) < type(0)); static const int num_of_bytes = sizeof(type); static const int num_of_bits = sizeof(type) * 8; }; template<unsigned bits_count, typename ut_t, typename udt_t, template <typename> class meta_t> struct basic_info< uint_t<bits_count, ut_t, udt_t, meta_t> > { static const bool is_native = false; static const bool is_signed = false; static const int num_of_bytes = bits_count / 8; static const int num_of_bits = bits_count; }; template<typename t> struct isizeof { static const int value = sizeof(t) * 8; }; template<typename t> struct num_of_dec_digits_in_type { static const int value = int(isizeof<t>::value * 0.301f); }; template <typename type> struct default_meta { static const int array_count = basic_info<type>::num_of_bits / isizeof<typename type::ut>::value; static const int dec_digits_count = num_of_dec_digits_in_type<type>::value; }; } int main() { typedef uint_t<128, unsigned short int, unsigned int, mxi::default_meta> uint128_t; int a = uint128_t::meta::dec_digits_count; int b = mxi::num_of_dec_digits_in_type<uint128_t>::value; }

GCC 4.8.1编译没有问题,但Visual C ++ 2008 SP1编译时出现以下错误:

line (40): error C2027: use of undefined type 'uint_t<bits_count,ut_t,udt_t,meta_t>' with [ bits_count=128, ut_t=unsigned short, udt_t=unsigned int, meta_t=mxi::default_meta ] line(46) : see reference to class template instantiation 'mxi::isizeof<t>' being compiled with [ t=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(54) : see reference to class template instantiation 'mxi::num_of_dec_digits_in_type<t>' being compiled with [ t=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(11) : see reference to class template instantiation 'mxi::default_meta<type>' being compiled with [ type=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(63) : see reference to class template instantiation 'uint_t<bits_count,ut_t,udt_t,meta_t>' being compiled with [ bits_count=128, ut_t=unsigned short, udt_t=unsigned int, meta_t=mxi::default_meta ]

如果uint_t是文件中定义的第一个类型,它是如何定义的?

注意:我已经将大量代码剥离到只有错误的部分,即这里的大多数数字是在编译时计算的,但我在这里直接用它们来关注问题

In the following code

template<unsigned bits_count, typename ut_t, typename udt_t, template <typename> class meta_t> struct uint_t { typedef uint_t<bits_count, ut_t, udt_t, meta_t> t; typedef ut_t ut; typedef udt_t udt; typedef meta_t<t> meta; ut comp[meta::array_count]; }; namespace mxi { template<typename type> struct basic_info { static const bool is_native = true; static const bool is_signed = (type(-1) < type(0)); static const int num_of_bytes = sizeof(type); static const int num_of_bits = sizeof(type) * 8; }; template<unsigned bits_count, typename ut_t, typename udt_t, template <typename> class meta_t> struct basic_info< uint_t<bits_count, ut_t, udt_t, meta_t> > { static const bool is_native = false; static const bool is_signed = false; static const int num_of_bytes = bits_count / 8; static const int num_of_bits = bits_count; }; template<typename t> struct isizeof { static const int value = sizeof(t) * 8; }; template<typename t> struct num_of_dec_digits_in_type { static const int value = int(isizeof<t>::value * 0.301f); }; template <typename type> struct default_meta { static const int array_count = basic_info<type>::num_of_bits / isizeof<typename type::ut>::value; static const int dec_digits_count = num_of_dec_digits_in_type<type>::value; }; } int main() { typedef uint_t<128, unsigned short int, unsigned int, mxi::default_meta> uint128_t; int a = uint128_t::meta::dec_digits_count; int b = mxi::num_of_dec_digits_in_type<uint128_t>::value; }

GCC 4.8.1 compiles without problem, but Visual C++ 2008 SP1 compile with the following error:

line (40): error C2027: use of undefined type 'uint_t<bits_count,ut_t,udt_t,meta_t>' with [ bits_count=128, ut_t=unsigned short, udt_t=unsigned int, meta_t=mxi::default_meta ] line(46) : see reference to class template instantiation 'mxi::isizeof<t>' being compiled with [ t=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(54) : see reference to class template instantiation 'mxi::num_of_dec_digits_in_type<t>' being compiled with [ t=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(11) : see reference to class template instantiation 'mxi::default_meta<type>' being compiled with [ type=uint_t<128,unsigned short,unsigned int,mxi::default_meta> ] line(63) : see reference to class template instantiation 'uint_t<bits_count,ut_t,udt_t,meta_t>' being compiled with [ bits_count=128, ut_t=unsigned short, udt_t=unsigned int, meta_t=mxi::default_meta ]

How uint_t is not defined when it's the first type defined in the file?

Note: I've stripped a lot of code to the only parts with error, i.e. most numbers here are computed at compile time, but I used them here directly to keep focus in the problem

最满意答案

好吧,当我使用完整的类型 - 即uint128_t时,我不明白为什么类型不完整 - 但我能找到两个解决方案:

1-在num_of_dec_digits_in_type使用isizeof<t>::value num_of_dec_digits_in_type替换isizeof<t>::value并执行此操作。

2-创建新类型num_of_dec_digits_in_type_size并使用它而不是num_of_dec_digits_in_type :

template<int size> struct num_of_dec_digits_in_type_size { static const int value = int(size * 0.301f); }; template <typename type> struct default_meta { . . static const int dec_digits_count = num_of_dec_digits_in_type_size< basic_info<type>::num_of_bits >::value; };

第二种解决方案只是对第一种解决方案的修改。

Well I didn't understand why the type is not complete when I'm using a complete type - i.e. uint128_t - but i was able to find two solutions:

1- In num_of_dec_digits_in_type replace isizeof<t>::value with basic_info<t>::num_of_bits and that will do it.

2- Create new type num_of_dec_digits_in_type_size and use it instead of num_of_dec_digits_in_type:

template<int size> struct num_of_dec_digits_in_type_size { static const int value = int(size * 0.301f); }; template <typename type> struct default_meta { . . static const int dec_digits_count = num_of_dec_digits_in_type_size< basic_info<type>::num_of_bits >::value; };

The 2nd solution is a just a modification of the 1st solution.

更多推荐

本文发布于:2023-08-07 04:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   类型   未定义   type   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!