如何解决同名函数和类之间的歧义情况
how to resolve ambiguous situation between same named function and class?
如果不调用test,这段代码编译没有任何问题,所以我得出结论,c++允许创建具有相同名称的类和函数:
class test {};
void test() {}
int main() {
test an_instance_of_test;
}
错误是:
<stdin>: In function 'int main()':
<stdin>:5:8: error: expected ';' before 'an_instance_of_test'
<stdin>:5:27: warning: statement is a reference, not call, to function 'test' [-Waddress]
我知道我不应该首先创建这样的明确性,但是这可能在其他人的代码中经历过,我问是否有一种方法可以在不改变函数或类定义的情况下摆脱这种情况。
您应该使用详细的类型说明符:
class test an_instance_of_test;
如标准所说(§3.4.4):
可以使用详细类型说明符(7.1.6.3)来引用先前声明的类名或enum-name,即使名称已被非类型声明隐藏。
名称查找简单地忽略所有非类型的名称:
标识符根据3.4.1查找,但忽略已声明的任何非类型名称。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 如何在没有映射的情况下在枚举和字符串之间进行转换?
- 在没有外部文件的情况下在应用会话之间保存变量
- 如何在不复制数据的情况下在平面数组和多维数组之间进行转换?
- 在没有比较运算符的情况下查找 2 个数字之间的最小值
- 在特定情况下,指针删除和铸造之间的关系不清楚
- Boost 在两个进程(C 和 C++)之间的managed_shared_memory使用情况
- 在给定某些约束的情况下,求出元素之间的最大距离
- RAII 是否可以在不同步的情况下有效地在线程之间共享不可变对象
- 如何在没有多余整数的情况下填充整数之间的分隔符
- 如何在不使用全局的情况下在函数之间使用多个变量
- 是否可以在不重新编译程序的情况下在BLAS库之间切换
- 在没有服务器的情况下在c++和HTML5/Javascript应用程序之间进行通信
- 为什么在多重定义的情况下char*和char[]之间存在差异
- 在基类和扩展类之间存在关系的情况下,没有匹配函数错误
- 有没有一种方法可以在没有while循环的情况下在计算机之间传输数据?c++
- 如何在没有数学函数(arctan)的情况下计算point和org(0,0)之间的快速路径角(最接近八个提供值之一)
- 如何解决同名函数和类之间的歧义情况
- 什么是对右值表达式和左值表达式之间差异的简洁、严格的定义,涵盖了所有可能的情况
- 在不使用tab键的情况下在Windows Mobile中的对话框控件之间移动