Declare in global namespace when defining a method in some namespace? - c++

My code contains two parts - a class template test inside namespace my, and a function frobnicate in a global namespace. The class template wants to use this function in one of its methods, and I want to declare it near its use:
namespace my
{
template <class T>
struct test
{
void do_stuff(T& i)
{
void frobnicate(T&);
frobnicate(i);
// more code here
}
};
}
struct SomeClass {};
void frobnicate(SomeClass& i)
{
// some code here
}
int main()
{
my::test<SomeClass> t;
SomeClass object;
t.do_stuff(object);
}
This works in MS Visual Studio 2017 due to a bug (described here), because Visual Studio declares the function in the global namespace, while according to the Standard, such declaration declares the function in current namespace.
However, gcc rightfully complains:
... undefined reference to `my::frobnicate(SomeClass&)'
Can I make it work in gcc or any other standard-compliant compiler?
If I put the declaration of frobnicate before declaring template test, the code works. However, in my real code, these two parts of code are in unrelated header-files, and I want my code to work regardless of the order of #includes, if possible.

Just forward declare your frobnicate function as a template before the namespace.
template <class T>
void frobnicate(T&);
namespace my {
template <class T>
struct test
{
void do_stuff(T& i)
{
frobnicate(i);
// more code here
}
};
} // close my namespace

I think you could simply declare frobnicate in seperate file and include it before creating namespace my.Or you could declare the function as a macro with #define in the same file. Then you can call frobnicate in the do_stuff in my.
Otherwise you can't call an undefined function.

Related

C++20 template compilation passes

I am having a problem with the changes that were made to the way C++ templates are compiled, between the C++17 and 19 standards. Code that used to compile in VS2017 throws a compiler error since I upgraded to VS2019 or VS2022.
Situations have to do with the fact that the compiler now runs a basic syntax check on the template definition when it sees this definition ("first pass") and not only when the template is actually used.
Code example 1:
class Finder
{
template<typename T>
T convert_to(HANDLE h)
{
return Converters::Converter<T>::Convert(get_data(h));
}
};
Here, the template class Converter<> resides in namespace Converters, and get_data is a member function of Finder which returns something that can be passed into the Convert function.
Since we're dealing with templates, this code sits in a header file "Finder.h". The header file doesn't #include "Converters.h". Finder.h is shared across several projects, some of which don't even know the Converters.h file namespace.
As long as no code calls the MyClass::convert_to<> function, this compiles in VS2017, but not so in VS2019 and VS2022:
error C3861: 'Converters': identifier not found
The obvious solution is, of course, to #include "Converters.h" either in this header file, or in the precompiled headers file. However, as was said, Converters.h is not known in all places which use MyClass. Another solution would be to use archaic #define CONVERTERS_H in the Converters.h header and enclose the function definition in #ifdef CONVERTERS_H, but this looks really ugly.
My question is: Is there a way to prevent the compiler from doing this "first pass"? Or to re-write this code so that it compiles? I don't mind if it's MS specific; no other compiler will ever see the code.
Code example 2:
class MyClass2
{
template<class T>
static void DoSomething(T* ptr) { static_assert(false, "Don't do this"); }
// lots more member functions, most of them 'static'
};
template<> void MyClass::DoSomething(CWnd* ptr) { /*some useful code*/ }
/// and some more specializations of DoSomething
The intention is that the static_assert should emit an error message whenever DoSomething is called with an argument for which no explicit specialization of this template function is defined. This worked in VS2017, but in VS2022, the "first pass" of the compiler triggers the static_assert.
Again, I wonder how I could achieve this effect, other than by replacing the static_assert by a run-time assertion.
Or am I thinking into a completely wrong direction?
Thanks
Hans
The first case requires a forward declaration of some kind, that's unavoidable.
The second case, though, can be handled with just a minor change.
#include <type_traits>
class CWnd {};
class MyClass2
{
public:
template<class T, class Y=T>
static void DoSomething(T* ptr) { static_assert(!std::is_same_v<Y,T>, "Don't do this"); }
};
template<> void MyClass2::DoSomething(CWnd* ptr) { /*some useful code*/ }
void foo()
{
int a;
CWnd b;
MyClass2::DoSomething(&a); // ERROR
MyClass2::DoSomething(&b); // OK
}
(partial answer)
To fix MyClass2, the usual trick is to make false depend on T, so that the first pass does not trigger the assert.
// dependent false
template <typename>
constexpr bool dep_false() { return false; }
class MyClass2
{
template<class T>
static void DoSomething(T* ptr) {
static_assert(dep_false<T>(), "Don't do this");
}
// lots more member functions, most of them 'static'
};
// specialization example
template<>
void MyClass2::DoSomething<int>(int* ptr) {
std::cout << "int* is OK\n";
}

Defining template class member functions in a namespace?

I looked around and tried to find an answer to this. Is it possible to define the member functions of a template class in a namespace within a cpp file? I get an error when I try to do it.
Here are the two files I am trying to compile.
ArrayList.hpp
template<typename T>
class ArrayList{
ArrayList();
~ArrayList();
}
ArrayList.cpp
#include "ArrayList.hpp"
namespace{
template<typename T>
ArrayList<T>::ArrayList(){
/* function body goes here */
}
ArrayList<T>::~ArrayList(){
/* function body goes here */
}
Compiler error
error: cannot define or
redeclare 'ArrayList<T>' here because namespace '' does not enclose
namespace 'ArrayList'
ArrayList<T>::ArrayList()
You need to declare your class in the same namespace as you define its member functions.
And you are missing a template<typename T> before your destructor.
namespace ANamespace
{
template<typename T>
class ArrayList
{
ArrayList();
~ArrayList();
};
template<typename T>
ArrayList<T>::ArrayList()
{
/* function body goes here */
}
template<typename T>
ArrayList<T>::~ArrayList()
{
/* function body goes here */
}
}
Is it possible to define the member functions of a template class in a namespace within a cpp file?
No, that's not possible. As with any other class declarations the member function declarations and their definitions must appear in the same namespace.
Also unnamed namespaces as used in your example will be seen only in that particular translation unit.
You generally can't put template definitions to separate translation units (unless you have particular specialization cases). Read more about this here please:
Why can templates only be implemented in the header file?
How to fix it?
Just move everything out to your header file and drop the unnamed namespace like this:
ArrayList.hpp
template<typename T>
class ArrayList{
ArrayList();
~ArrayList();
}; // Semicolon at end of class declaration is essential
template<typename T>
ArrayList<T>::ArrayList(){
/* function body goes here */
}
template<typename T> // <<<<< Repeat for evey function definition
ArrayList<T>::~ArrayList(){
/* function body goes here */
}
If you really want to have a namespace, just provide a named one and enclose everything from above (still in the header):
namespace MyUtilities {
template<typename T>
class ArrayList{
public: // <<<<<< Allow access to these functions
ArrayList();
~ArrayList();
};
template<typename T>
ArrayList<T>::ArrayList(){
/* function body goes here */
}
template<typename T>
ArrayList<T>::~ArrayList(){
/* function body goes here */
}
}
See the fully working example here please.
Also to make that stuff usable use the public keyword as shown above.
Firstly you cannot define a template class's methods in a .cpp file. You need to define template class methods in the header file itself (or in another file and then import that via preprocessor includes in the header file itself) For more see Why can templates only be implemented in the header file?
Anonymous namespaces namespace { are used to define things that have internal linkage, i.e. similar to the effect static has on functions defined in implementation files. Read this for more information Why are unnamed namespaces used and what are their benefits?
But in your case you want to define the class methods to be externally visible because you have them declared publicly in the header file. So you probably don't want to have those definitions be in an anonymous namespace.
If you have a namespace enclosing your class in your header file, for example namespace array_list then you should just define your class methods in that same namespace in the header file itself since it's a template class and needs to be visible wherever used.

Is there a way to call an initialization function only when specific class template (specialization) is instantiated?

I'm designing a wrapper over various computational functionality. Some of the underlying backends require some init functions to be called before any other API calls are made. I could use some static variable that is initialized before main, and wrap it in some function as described here so that I can catch any errors produced during initialization.
I wonder if there is a better way to handle this. Note that there will never be an instance of the class template, as everything is either a typedef or static member.
To address the problem of initializing the API only for some specializations, and of initializing it only once, I'd do something like this:
#include <iostream>
template <typename T>
struct Wrapper
{
// class who will be statically instantiated
struct CtorClass
{
CtorClass()
{
std::cout << "Init\n";
}
};
static CtorClass static_ctor;
static void compute1(){}
static void compute2(){}
};
// definition for template static member cons
template <typename T>
typename Wrapper<T>::CtorClass Wrapper<T>::static_ctor;
struct NeedInit{};
// you will have to use static_ctor in every funcition of the
template <>
void Wrapper<NeedInit>::compute1()
{
static_ctor;
}
template <>
void Wrapper<NeedInit>::compute2()
{
static_ctor;
}
int main()
{
Wrapper<int>::compute1();
Wrapper<int>::compute2();
Wrapper<NeedInit>::compute1();
Wrapper<NeedInit>::compute2();
}
Sadly, this way you have to use static_ctor in every function specialization that belongs to a Wrapper<NeedInit> class. But you wouldn't need to check for the initialization to have already been called.
Then, you could catch errors like you said.

Why are function template specializations not allowed inside a class?

After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me!
My problem is that I want to do an explicit templatization of a function inside a class in C++. My compiler (g++) and a look in the C++ standard (§14.7.3) tells me that this specialization has to be done in the namespace in which the class is declared. I understand that this implies that I cannot put the specialization inside the class, but I don't see the point of this restriction! Does anyone know if there is a good reason for not letting the specializations be made inside the class?
I know that there are workarounds, e.g. to put the function inside a struct, but I want to understand why the language has this design. If there is a good reason for not allowing specialized functions inside the class, I guess I should know it before trying to work around it.
Thanks in advance!
To make my question a little bit more precise: Here is some code from a test example which illustrates what I want to do:
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {
privateVariable = 5;
};
virtual ~SpecializationTest() {};
void execute() {
execute<DIMENSIONALITY>();
};
private:
int privateVariable;
template <size_t currentDim>
static void execute() {
printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
execute<currentDim-1>();
}
template <>
static void execute<0>() {
printf("This is the base case. Current dim is 0.\n");
}
};
This is not possible; g++ says:
SpecializationTest_fcn.h:27: error: explicit specialization in non-namespace scope ‘class MalinTester::SpecializationTest<DIMENSIONALITY>’
SpecializationTest_fcn.h:28: error: template-id ‘execute<0>’ in declaration of primary template
If I put the function execute outside the class, in the name space MalinTester, it will look like this:
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY> class SpecializationTest {};
template <size_t currentDim>
void execute() {
printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
execute<currentDim-1>();
}
template <>
void execute<0>() {
printf("This is the base case. Current dim is 0.\n");
}
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {};
virtual ~SpecializationTest() {};
void execute() {
MalinTester::execute<DIMENSIONALITY>();
};
private:
int privateVariable = 5;
};
};
};
and I cannot use privatevariable in the templatized versions of execute, as it is private in the class. I really want it private, as I want to have my data encapsulated as far as possible.
Of course I can send privateVariable as an argument to the function, but I think it would be more beautiful to avoid this, and what I really wonder is if there is a good reason for the C++ standard not to allow explicit specialization as in the first code example above.
#Arne Mertz: This is the workaround I have tried, but it doesn't allow using privateVariable either. And most of all, I wonder if it is a good idea to do like this. As I'm not allowed to make specializations of member functions, maybe I shouldn't do specializations of functions encapsulated in structs inside the class either.
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {
privateVariable = 5;
};
virtual ~SpecializationTest() {};
void execute() {
Loop<DIMENSIONALITY, 0>::execute();
};
private:
int privateVariable;
template <size_t currentDim, size_t DUMMY>
struct Loop {
static void execute() {
printf("This is the general case. Current dim is %d.\n", currentDim);
Loop<currentDim-1, 0>::execute();
}
};
template <size_t DUMMY>
struct Loop<0, DUMMY> {
static void execute() {
printf("This is the base case. Current dim is 0.\n");
}
};
};
};
Base specialization:
In .h:
template <class T>
class UISelectorSlider : public UISelectorFromRange<T> {
public:
UISelectorSlider();
virtual ~UISelectorSlider();
private:
float width;
float getPositionFromValue(T value);
};
In .cpp under same namespace:
template <>
float UISelectorSlider<MVHue>::getPositionFromValue(MVHue value)
{
return width * (float)value / 360.0;
}
If you want specialized function within specialized class:
Inside class add (.h) (private function):
private:
template <int I>
void foo();
Specialization inside .cpp:
template <>
template <>
void UISelectorSlider<MVHue>::foo<3>()
{
// you can access private fields here
}
UPDATE:
But you cant write something like this:
template <class T>
template <>
void UISelectorSlider<T>::foo<3>()
{
// you can access private fields here
}
You will get: error: enclosing class templates are not explicitly specialized.
It does not matter is this definition inside class or in namespace. The point is that this is not exact partial specialization - this function does not have defined context class (which members you want to call). In other words - when you specialize member you actually try specialize the whole containing class, but not the member itself. And compiler cant do that because class is not yet defined completely. So this is restriction by template design. And if it actually worked - templates would be full equivalent to simple macros.
(And you probably can will solve your task with some macro magic.)

Calling a C++ namespace-qualified function within a template function in another namespace

If I have a default function 'foo' defined in a namespace in one header file:
//DefaultFoo.h
namespace DefaultFooNamespace
{
template <typename T>
void foo(T& x){/*...*/}
}
and overloads of foo defined in another namespace in one or more other header files, e.g., FooOverloads.h:
//FooOverloads.h
namespace FooOverloadsNamespace
{
template <typename T>
void foo(std::vector<T>& x){/*...*/}
void foo(std::string& x){/*...*/}
//etc.
}
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):
//Bar.h
#include "DefaultFoo.h"
namespace BarNamespace
{
template <typename T>
void bar(T& x)
{
//... do stuff then call foo
using DefaultFooNamespace::foo;
using FooOverloadsNamespace::foo;
foo(x);
}
}
'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.
//Dummy.h:
struct DummyClass
{
private:
DummyClass(){}
};
namespace FooNamespace
{
inline void foo(DummyClass& x); //purely to open up namespace
}
//Bar.h
#include "Dummy.h"
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?
My solution would be to get rid of the useless DefaultFooNamespace namespace. Put the default foo in the same namespace as the overloads, then bar just does:
using FooNamespace::foo;
foo(x);
The default foo is used if the caller hasn't included a definition of a more appropriate one for the type of x
I don't see a reason for the default foo to be in a separate namespace, especially because doing that requires a separate dummy definition to make bar valid code, when the same overload could serve both purposes.