Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a C++ namespace-qualified function within a template function in another namespace
    primarykey
    data
    text
    <p>If I have a default function 'foo' defined in a namespace in one header file:</p> <pre><code>//DefaultFoo.h namespace DefaultFooNamespace { template &lt;typename T&gt; void foo(T&amp; x){/*...*/} } </code></pre> <p>and overloads of foo defined in another namespace in one or more other header files, e.g., FooOverloads.h:</p> <pre><code>//FooOverloads.h namespace FooOverloadsNamespace { template &lt;typename T&gt; void foo(std::vector&lt;T&gt;&amp; x){/*...*/} void foo(std::string&amp; x){/*...*/} //etc. } </code></pre> <p>and I have another function 'bar' which calls foo after bringing both DefaultFooNamespace and FooOverloadsNamespace namespaces into scope (note that I #include only DefaultFoo.h in Bar.h, as DefaultFoo.h contains a single function in a namespace which is intended never to be extended, unlike FooOverloadsNamespace which will be extended by the addition of additional overloads):</p> <pre><code>//Bar.h #include "DefaultFoo.h" namespace BarNamespace { template &lt;typename T&gt; void bar(T&amp; x) { //... do stuff then call foo using DefaultFooNamespace::foo; using FooOverloadsNamespace::foo; foo(x); } } </code></pre> <p>'bar' won't compile unless I either make sure FooOverloads.h is #include'd before a #include of Bar.h, or I make sure that FooOverloads.h is #include'd in Bar.h, or alternatively I provide a declaration of a 'foo' function for a dummy class in FooNamespace and #include that in Bar.h, e.g.</p> <pre><code>//Dummy.h: struct DummyClass { private: DummyClass(){} }; namespace FooNamespace { inline void foo(DummyClass&amp; x); //purely to open up namespace } //Bar.h #include "Dummy.h" </code></pre> <p>Is there any way around this, such that I can define bar and avoid having to create redundant code to open up FooNamespace in Bar?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload