模板 能否按区间 进行特化

zilaishuichina 2014-07-08 03:14:18

template <int size>
class A //有一个模板
{
char arr[size];
}

template <>
class A<0> //size为0的特化
{
char arr[1];
}

template <>
class A</*能不能有一种方法可以按区间特化,比如 (<= 0) || (>= 0xffff) */>
{
char arr[1];
}
template <>
class A</*比如 (1000 <=) && (<= 9999)*/>
{
char arr[4];
}


我的主要意图是,有没有这样一种写法,或者说就是一个语法糖
能够对于 比如 A<-10> a; 这种我不希望出现的情况, 可以让编译器提示出错,
或者是对于非法的值能特化成默认的合法情况,比如A<-10> a特化成char arr[1];

这里arr我不希望定义成指针,
因为我想重载[]运算符,返回一个char& 这样的引用,若arr是指针,当运行时arr在某种情况下分配内存失败,是空指针的时候,[]运算符就不好再返回引用了。
...全文
186 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
unituniverse2 2014-07-09
  • 打赏
  • 举报
回复
A的末尾大括号的分号漏掉了
unituniverse2 2014-07-09
  • 打赏
  • 举报
回复
上面各位的建议我就不重复了。不过对于楼主目前的需求,用不上特化:

template <size_t size> 
class A //有一个模板
{
    char arr[size<= 0 || size >= 0xffff ? 1
        : (size>=1000 && size<=9999 ? 4
        : size)];
}
这里面数据类型用得是size_t,因此不会出现负数的情况。如果想对于一些特殊值报错的话,可以用static_assert。
mujiok2003 2014-07-08
  • 打赏
  • 举报
回复
引用 6 楼 mujiok2003 的回复:
试试这个
或者默认类型
mujiok2003 2014-07-08
  • 打赏
  • 举报
回复
menzi11 2014-07-08
  • 打赏
  • 举报
回复

template <int size,bool isSizeInRange=(size>0)> 
class A //有一个模板
{
    char arr[size];
}
 
template <int size>
class A<size,false> 
{
    ...............
}

template <int size>
class A<size,false> 
{
    ...............
}
 
template <int size>
class A<size,true> 
{
    ...............
}

int main()
{
    A<100> a;
   return 1;
}

baihacker 2014-07-08
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
struct ZeroTag{};
struct InvalidTag{};
struct GeneralTag{};
struct GeneralTag1{};

template <int size, typename Tag> 
class A;

template <int size>
class A<size, ZeroTag>
{
    char arr[1];
public:
	void show(){cerr<<1<<endl;}
};

template <int size>
class A<size, InvalidTag>
{
    char arr[1];
public:
	void show(){cerr<<2<<endl;}
};

template <int size>
class A<size, GeneralTag>
{
    char arr[size];
public:
	void show(){cerr<<3<<endl;}
};

template <int size>
class A<size, GeneralTag1>
{
    char arr[4];
public:
	void show(){cerr<<4<<endl;}
};

template<bool flag, typename A, typename B>
struct the_if;

template<typename A, typename B>
struct the_if<true, A, B>
{
	typedef A result_type;
};
template<typename A, typename B>
struct the_if<false, A, B>
{
	typedef B result_type;
};

template<int size>
struct Helper
{
	typedef typename the_if<size==0, ZeroTag,
				typename the_if<(size<= 0 || size >= 0xffff), InvalidTag,
					typename the_if<(size>=1000&&size<=9999), GeneralTag1, GeneralTag>::result_type
				>::result_type
			>::result_type
			the_tag;
	typedef A<size, the_tag> ResultType;
};

int main()
{
	Helper<0>::ResultType test0;
	Helper<1>::ResultType test1;
	Helper<0x10000>::ResultType test2;
	Helper<-1>::ResultType test3;
	Helper<1000>::ResultType test4;
	test0.show();
	test1.show();
	test2.show();
	test3.show();
	test4.show();
	
	return 0;
}
zilaishuichina 2014-07-08
  • 打赏
  • 举报
回复
自己顶一下
zilaishuichina 2014-07-08
  • 打赏
  • 举报
回复
引用 1 楼 lunaflywar 的回复:
构造函数里判断了new不就可以了
arr我不希望定义成指针,理由见上
lunaflywar 2014-07-08
  • 打赏
  • 举报
回复
构造函数里判断了new不就可以了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧