c 11 – 从一个模板实例转换为另一个模板实例

c 11 – 从一个模板实例转换为另一个模板实例,第1张

概述我有一个固定大小的字符串类定义如下: template <typename const std::size_t max_size>class fixed_string {...}; 此类保留一个char缓冲区来保存max_size字符. 我希望能够将此类的对象传递给采用模板实例化的方法,该模板实例化具有较低的max_size模板参数值而无需任何复制.所以例如以下方法: void foo(co @H_502_2@ 我有一个固定大小的字符串类定义如下:

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限制,可以再次仅从足够长的字符串文字创建引用.

@H_502_2@ 总结

以上是内存溢出为你收集整理的c 11 – 从一个模板实例转换为另一个模板实例全部内容,希望文章能够帮你解决c 11 – 从一个模板实例转换为另一个模板实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1227855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存