千家信息网

C+为什么避免定义没有明确语义的概念

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容介绍了"C+为什么避免定义没有明确语义的概念"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!T
千家信息网最后更新 2026年11月02日C+为什么避免定义没有明确语义的概念

本篇内容介绍了"C+为什么避免定义没有明确语义的概念"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

T.20:避免定义没有明确语义的"概念"

Reason(原因)

Concepts are meant to express semantic notions, such as "a number", "a range" of elements, and "totally ordered." Simple constraints, such as "has a + operator" and "has a > operator" cannot be meaningfully specified in isolation and should be used only as building blocks for meaningful concepts, rather than in user code.

概念意在表现某种有语义的观念,例如"数字",元素的"范围",和"完全有序"等。简单的约束,例如有+运算符,有>运算符不能算作被独立,明确地定义,只应用于某个明确概念的组成部分,而不是在代码中直接使用。

Example, bad (using TS concepts)

反面示例(使用TS概念)

template
concept Addable = has_plus; // bad; insufficient

template auto algo(const N& a, const N& b) // use two numbers
{
// ...
return a + b;
}

int x = 7;
int y = 9;
auto z = algo(x, y); // z = 16

string xx = "7";
string yy = "9";
auto zz = algo(xx, yy); // zz = "79"

Maybe the concatenation was expected. More likely, it was an accident. Defining minus equivalently would give dramatically different sets of accepted types. This Addable violates the mathematical rule that addition is supposed to be commutative: a+b == b+a.

也许期待的操作就是连结。更有可能的是,这只是一个意外。对等地定义减操作将会提供一套明显不同的可接受类型。这个可加性违反了加法运算满足交换律(a+b==b+a)这个规则。

Note(注意)

The ability to specify a meaningful semantics is a defining characteristic of a true concept, as opposed to a syntactic constraint.

定义明确语义的能力是真正的概念所具备的明确特征,而不是句法约束。

Example (using TS concepts)

示例(使用TS概念)

template
// The operators +, -, *, and / for a number are assumed to follow the usual mathematical rules
concept Number = has_plus
&& has_minus
&& has_multiply
&& has_divide;

template auto algo(const N& a, const N& b)
{
// ...
return a + b;
}

int x = 7;
int y = 9;
auto z = algo(x, y); // z = 16

string xx = "7";
string yy = "9";
auto zz = algo(xx, yy); // error: string is not a Number
Note(注意)

Concepts with multiple operations have far lower chance of accidentally matching a type than a single-operation concept.

相比只有一个操作的概念,包含多个操作的概念偶然和某个类型匹配的可能性会大为降低。

Enforcement(实施建议)

  • Flag single-operation concepts when used outside the definition of other concepts.

  • 标记处于其他概念定义范围之外的单操作概念。

  • Flag uses of enable_if that appears to simulate single-operation concepts.

  • 标记将enable_if用于模拟单操作概念的情况。

"C+为什么避免定义没有明确语义的概念"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

概念 语义 运算 内容 情况 更多 标记 知识 示例 类型 范围 运算符 不同 实用 明显 有序 意外 学有所成 接下来 代码 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网络科技 互联网科技海报 百单网互联网科技有限公司 互联网科技公司经营范围 互联网科技战争 云电互联网科技 卓越科技互联网 招聘 四个字互联网科技公司取名 无锡溥仪互联网科技有限公司 北京大学互联网科技怎么样 荆州市昕露互联网络科技有限公司 花神(南京)互联网科技有限公司 成都互联网科技公司工作好不好 亳州好赞互联网科技有限公司 西安小鹰速贷互联网科技有限公司 互联网科技和法律行业 深圳艾果互联网通讯科技 卡尔迅互联网科技有限公司 互联网公司属于科技吗 银川新华互联网科技学校原画 科技类互联网 唐信互联网科技 宁夏科技产业互联网案例 银行科技岗属于互联网吗 卡尔迅互联网科技有限公司 云鲸互联网络科技有限公司 互联网对科技影响 现代互联网科技公司 平安好医生打造互联网科技医疗 优雇互联网科技有限公司
0