Too few arguments in function call while trying to call function with the same name from another namespace

ぐ巨炮叔叔 提交于 2020-01-03 02:01:53

问题


I'm getting a "too few arguments in function call" while trying to call a function from another namespace that happens to have the same name with the function it's being called from. Here, you can see what I mean:

namespace doa::texture {
   using namespace internal::texture;

      Texture* const CreateTexture(const std::string& name, const std::string& pathToTextureImage) {
         //below should call internal::texture::CreateTexture, not doa::texture::CreateTexture
         Texture* texture{ CreateTexture(pathToTextureImage) };
         ... //implementation detail
      }
}

namespace internal::texture {

   Texture* const CreateTexture(const std::string& pathToTextureImage) { ... }
}

I can easily fix the error by slapping an internal::texture in front of the function call but since I'm using the using namespace internal::texture directive, the compiler should be able to pick it up.

I can also move the CreateTexture function from the namespace internal::texture to doa::texture. But I would like to avoid that.

How can I fix this without moving the function or putting internal::texture in front of the call, and why this thing happens in the first place? This is simply a case of function overloading, why do namespaces cause such a thing to happen? Thank you.

P.S. The functions are defined in a separate file. like this:

//irrelevant implementation details are left out
namespace doa::texture {

   Texture* const CreateTexture(const std::string& name, const std::string& pathToTextureImage);
}

namespace internal::texture {

   Texture* const CreateTexture(const std::string& pathToTextureImage);
}

回答1:


I'm going to try to put a few pieces of the C++17 standard (draft) together, and hope I'm not misinterpretting it :)

From the unqualified name lookup:

1 In all the cases listed in 6.4.1, the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name. If no declaration is found, the program is ill-formed.

2 The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see 10.3.4. For the purpose of the unqualified name lookup rules described in 6.4.1, the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.

From using directive

2 A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (6.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [Note: In this context, “contains” means “contains directly or indirectly”. —end note]

From Name Hiding

1 A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (13.2).

Specifically, with this last part, I believe it means that the CreateTexture function in doa::texture hides the CreateTexture in internal::texture even though it is within a different namespace. Basically, the compiler sees CreateTexture in the doa::texture namespace, then stops looking, and hence ignored the one from internal::texture



来源:https://stackoverflow.com/questions/59121445/too-few-arguments-in-function-call-while-trying-to-call-function-with-the-same-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!