加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C中的元编程中,保护从非const-volatile类型到const-volatile的

发布时间:2020-12-16 07:20:42 所属栏目:百科 来源:网络整理
导读:例如,我有一个实现null_or的函数 template typename T,typename Uauto null_or(T *p,U *default_value) - typename std::enable_ifstd::is_sametypename std::decayU::type,typename std::decayT::type::value,T *::type { if (p) { return p; } else { retu
例如,我有一个实现null_or的函数

template <typename T,typename U>
auto null_or(T *p,U *default_value) ->
    typename std::enable_if<std::is_same<typename std::decay<U>::type,typename std::decay<T>::type>::value,T *>::type {
  if (p) {
    return p;
  } else {
    return default_value;
  }
}

仅使用std :: decay将使const / volatile类型* default_value分配到非const / non-volatile类型* p.

避免它的最佳方法是什么?

此外,类型default_value [100]的数组类型(具有范围)不能分配给它.怎么解决呢?

可以也可以没有例子:

const char *s = nullptr;
  char       *s2 = nullptr;

  const char *hello0 = "hello0";
  std::string hello1 = "hello1";
  char        hello2[] = "hello2";
  char        hello3[100] = "hello3";
  int         hello4[] = {1,2,3,4,5,0};

  const char *ss = nullptr;

  // const char * ==> const char *,OK
  ss = null_or(s,hello0);
  printf("%sn",ss);

  // std::string ==> const char *,no conversion at all,Bad
  // ss = null_or(s,hello1);
  // printf("%sn",ss);

  // char [7] ==> const char *,hello2);
  printf("%sn",ss);

  // char [100] ==> const char *,hello3);
  printf("%sn",ss);

  // int [6] ==> const char *,hello4);
  // printf("%sn",ss);

  // const char * ==> char *,const pointer stuffs should not be assigned to non consts,Bad
  // ... also,const reference should not be assigned to non const references for some other generic algorithms
  // ss = null_or(s2,hello0);
  // printf("%sn",ss);

  // std::string ==> char *,no version at all,Bad
  // ss = null_or(s2,ss);

  // char [7] ==> char *,OK
  ss = null_or(s2,ss);

  // char [100] ==> char *,ss);

  // int [6] ==> char *,ss);

还有很多其他通用算法只需要简单的概念检查右侧是否可以安全地分配到左侧,所以我想知道是否有更通用的解决方案.

解决方法

您的示例在需要时已经失败,但根据null_or定义.

您可以将声明更改为

template <typename T,U *default_value)
-> typename std::enable_if<std::is_assignable<T*&,U*>::value,T *>::type

到SFINAE那些错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读