简体   繁体   English

当同一个命名空间中有两个具有相同签名的函数时会发生什么?

[英]What happens when there are two functions with the same signature in the same namespace?

As far as I understand, I can use namespaces to avoid functions overriding each other.据我所知,我可以使用命名空间来避免函数相互覆盖。 But what if the namespaces have the same names?但是如果命名空间具有相同的名称呢?

I included both of the .h files below, called VideoLibrary::check() and it compiles.我包括了下面的两个 .h 文件,称为 VideoLibrary::check() 并编译。 Why?为什么? Doesn't it contradict the purpose of namespace?这不是与命名空间的目的相矛盾吗?

file1.h and file2.h: file1.h 和 file2.h:

namespace VideoLibrary
{
  void check();
}

file1.cpp:文件1.cpp:

namespace VideoLibrary
{
  void check()
    {
        printf("check1");
    }
}

file2.cpp:文件2.cpp:

namespace VideoLibrary
{
  void check()
    {
        printf("check2");
    }
}

I'm concerned mostly because my project includes external libraries, and i'm worrying it includes functions with the same signature as in my project.我担心主要是因为我的项目包含外部库,我担心它包含与我的项目具有相同签名的函数。 How can I get any error for such a case in compilation time?我怎样才能在编译时得到这种情况的任何错误? I'm using Xcode as an editor if it matters.如果重要的话,我将 Xcode 用作编辑器。

如果您在不同的翻译单元中有 2 个(非内联)相同函数的定义,则您有 ODR(一个定义规则)违反 NDR(无诊断要求)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当两个共享库定义相同的符号时,实际会发生什么? - What actually happens when two shared libraries define the same symbol? 在C ++中定义具有相同签名的两个函数 - Define two functions with the same signature in C++ 在C ++中,如果两个不同的函数声明相同的静态变量会发生什么? - In C++, what happens if two different functions declare the same static variable? 当两个线程同时在SAME互斥体上尝试try_lock()时,会发生什么情况? - What happens when two threads attempt a try_lock() on the SAME mutex at the EXACT same time? 在同一个名称空间中使用函数 - Using functions in the same namespace 从命名空间获取具有相同签名的函数,并在C ++中迭代它们 - Getting functions with the same signature from a namespace and iterating over them in C++ 如何静态检查可能具有或可能不具有相同签名的两个函数是否相同 function? - How to statically check if two functions that might or might not have the same signature are the same function? ld链接同一库的不同版本时会发生什么 - What happens when ld link different versions of the same library 当成为朋友的类具有相同的名称成员变量时会发生什么 - What happens when Classes that are friends have same name member variables 同时从不同线程向套接字写入套接字时会发生什么? - What happens when writing to a socket from different threads at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM