c 11 – 从一个模板实例转换为另一个模板实例
发布时间:2020-12-16 07:13:55 所属栏目:百科 来源:网络整理
导读:我有一个固定大小的字符串类定义如下: template typename const std::size_t max_sizeclass fixed_string {...}; 此类保留一个char缓冲区来保存max_size字符. 我希望能够将此类的对象传递给采用模板实例化的方法,该模板实例化具有较低的max_size模板参数值
我有一个固定大小的字符串类定义如下:
template <typename const std::size_t max_size> class fixed_string { ... }; 此类保留一个char缓冲区来保存max_size字符. 我希望能够将此类的对象传递给采用模板实例化的方法,该模板实例化具有较低的max_size模板参数值而无需任何复制.所以例如以下方法: void foo(const fixed_string<50>& val) { } 应该使用fixed_string< 100>来调用,以便以下工作: void bar() { fixed_string<100> s = "gg"; foo(s); } 我怎么能这样做? 解决方法
假设编译器以类似的方式将它们放置出来,第一直觉就是从大到小的fixed_strings重新解释,但由于C的严格别名规则,这将是非法的(对象可能永远不会被引用或不同类型的指针访问),除非其中一个是[signed | unsigned] char)
您可以通过创建自定义参考模板来解决此问题,fixed_strings可以隐式转换为: #include <type_traits> template<std::size_t N> struct fixed_string_ref { char *string; }; template<std::size_t N> struct fixed_string_const_ref { const char *string; }; template<std::size_t N> struct fixed_string { char string[N]; template<std::size_t M,typename std::enable_if<(M < N),int>::type = 0> operator fixed_string_const_ref<M>() const { return fixed_string_const_ref<M> { string }; } template<std::size_t M,int>::type = 0> operator fixed_string_ref<M>() { return fixed_string_ref<M> { string }; } }; void foo(fixed_string_const_ref<10>) {} int main() { fixed_string<20> f; foo(f); //fixed_string<5> g; //foo(g); <- cannot be converted to fixed_string_const_ref<10> } fixed_string [_const] _ref只保存一个指向fixed_string数据的指针,因此可以通过值传递而不复制字符串.需要有两个这样的类型,一个是const,一个是非const,以保持const正确性. enable_if部分确保只能创建对较小或相等的fixed_strings的引用. 可选:通过向fixed_string_const_ref添加两个构造函数,普通字符串文字也可以作为固定字符串引用传递. template<std::size_t N> struct fixed_string_const_ref { const char *string; explicit fixed_string_const_ref(const char *s) : string(s) {} template<std::size_t M,typename std::enable_if<(M >= N),int>::type = 0> fixed_string_const_ref(const char (&s)[M]) : string(s) {} }; // ... int main() { // .... foo("Hello World"); //foo("Hello"); <- cannot be converted to fixed_string_const_ref<10> } 通过使第一个显式显示而第二个通过enable_if限制,可以再次仅从足够长的字符串文字创建引用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |