template functions with shared private data - c++

I'm looking for 'best-practice' in the following situation:
In general, there are three common ways to share private data between two (or more) non-member functions with differential advantages and disadvantages:
// Example 1: using 'static' class
// HPP
namespace foo {
class bar
{
private:
static const char* const s_private;
bar();
public:
static void s_method0();
static void s_method1();
}; /* class bar */
} /* namespace foo */
// CPP
namespace foo {
const char* const bar::s_private = "why do I need to be visible in HPP?";
void bar::s_method0() { std::cout << "s_method0 said: " << s_private << std::endl; }
void bar::s_method1() { std::cout << "s_method1 said: " << s_private << std::endl; }
} /* namespace foo */
// Example 2: using unnamed-namespace
// HPP
namespace foo {
void bar0();
void bar1();
} /* namespace foo */
// CPP
namespace foo {
namespace {
const char* const g_anonymous = "why do I need external linkage?";
} /* unnamed-namespace */
void bar0() { std::cout << "bar0 said: " << g_anonymous << std::endl; }
void bar1() { std::cout << "bar1 said: " << g_anonymous << std::endl; }
} /* namespace foo */
// Example 3: using static keyword in namespace-scope
// HPP
namespace foo {
void bar0();
void bar1();
} /* namespace foo */
// CPP
namespace foo {
static const char* const g_internal = "nobody outside this file can see me and I don't have external linkage";
void bar0() { std::cout << "bar0 said: " << g_internal << std::endl; }
void bar1() { std::cout << "bar1 said: " << g_internal << std::endl; }
} /* namespace foo */
I prefer 'Example 3' because it's as close to the intention as it could be.
But now I'm running in some problem's using templated functions. 'Example 1' seems to be the only way to solve this:
// HPP
namespace foo {
class bar
{
private:
static const char* const s_private;
bar();
public:
template<typename T> static void s_method0() { std::cout << "s_method0 said: " << s_private << std::endl; }
template<typename T> static void s_method1() { std::cout << "s_method1 said: " << s_private << std::endl; }
}; /* class bar */
} /* namespace foo */
// CPP
namespace foo {
const char* const bar::s_private = "why do I need to be visible in HPP?";
} /* namespace foo */
That's unsatisfying. Especialy because there are other (in this case methods) non-member function which should be in the same (in this case class-) scope, which don't need to access this private data.
Does anybody know an elegant solution?
Thanks for any help.
Best regards.

This is, somewhat unfortunately, an issue that springs quite often with template.
But may I suggest that you are over-engineering here ?
The truth is, whether you look at Loki code (by Andrei Alexandrescu) or Boost code (infamous David Abrahams notably), no-one really bothered to provide a better privacy.
Rather, they simply relied on convention and used a Private namespace (Loki) or a detail namespace (Boost, with sometimes a longer and more descriptive name to prevent clashes).
It's annoying, but there is not much you can do in practice.... though I actually have a solution for your specific problem ;)
// Evil solution!
#ifdef MY_SUPER_MACRO
# error "MY_SUPER_MACRO is already defined!"
#endif
#define MY_SUPER_MACRO "Some string"
template <typename T> void foo() { std::cout << "foo - " MY_SUPER_MACRO "\n"; }
template <typename T> void bar() { std::cout << "bar - " MY_SUPER_MACRO "\n"; }
#undef MY_SUPER_MACRO
And hop, I achieved locality in a header with an evil macro :)

If I understand correctly, your complaint/concern is that unlike with templates, with non-templates, one can define the function bodies inside the CPP, rather than the header, in which case they can access non-class statics, which are "invisible" to the outside world, including member functions defined in the header. This is all true.
However, remember that there's nothing stopping one defining other member functions in the CPP as well, in which case, they'd be equally able to access the static data. So really, the situation is no different. Your complaint is based on a false dichotomy.
If you genuinely want to prevent anything but s_method0<T>() and s_method1<T>() accessing s_private, then you must put them all in a dedicated class. It's as simple as that. This would be the case even for non-templates.

I had played around with the different techniques. My idea, using the unnamed namespace in the header file, was to 'mark' the 'shared'-classes as 'header-file-only'. Sure, due to the fact they aren't containing public members you cannot make nasty things anyway with it. But I thought it would be more close to the intention.
But I was wrong! After thinking about this I'm in shame. It's so logically simple. This example is showing what's the problem with it (entire code for clearness):
// header0.hpp
#ifndef HPP_HEADER0_INCLUDED
#define HPP_HEADER0_INCLUDED
#include <iostream>
#include <string>
namespace ns {
template<typename T> void header0_func0();
template<typename T> void header0_func1();
namespace {
class header0
{
template<typename> friend void ns::header0_func0();
template<typename> friend void ns::header0_func1();
static std::string s_private;
}; /* class header0 */
} /* unnamed */
template<typename T> void header0_func0() { std::cout << "header0_func0: " << header0::s_private << std::endl; }
template<typename T> void header0_func1() { std::cout << "header0_func1: " << header0::s_private << std::endl; }
} /* namespace ns */
#endif /* HPP_HEADER0_INCLUDED */
// header1.hpp
#ifndef HPP_HEADER1_INCLUDED
#define HPP_HEADER1_INCLUDED
#include <iostream>
#include <string>
namespace ns {
template<typename T> void header1_func0();
template<typename T> void header1_func1();
namespace {
class header1
{
template<typename> friend void ns::header1_func0();
template<typename> friend void ns::header1_func1();
static std::string s_private;
}; /* class header1 */
} /* unnamed */
template<typename T> void header1_func0() { std::cout << "header1_func0: " << header1::s_private << std::endl; }
template<typename T> void header1_func1() { std::cout << "header1_func0: " << header1::s_private << std::endl; }
} /* namespace ns */
#endif /* HPP_HEADER1_INCLUDED */
// source.cpp
#include "header0.hpp"
#include "header1.hpp"
std::string ns::header0::s_private = "header0 private data definition by source.cpp",
ns::header1::s_private = "header1 private data definition by source.cpp";
namespace {
// hide private class
class source
{
source();
~source();
static source s_instance;
};
source::source() {
std::cout << "source.cpp:\n";
ns::header0_func0<int>();
ns::header0_func1<int>();
ns::header1_func0<int>();
ns::header1_func1<int>();
std::cout << '\n';
}
source::~source() { }
source source::s_instance;
} /* unnamed */
By now everything seems to be OK. But what happen's if we try to use our headers in other translation units? Let's take a look:
// main.cpp
#include "header0.hpp"
#include "header1.hpp"
int main()
{
std::cout << "main.cpp:\n";
ns::header0_func0<int>();
ns::header0_func1<int>();
ns::header1_func0<int>();
ns::header1_func1<int>();
std::cout << '\n';
return 0;
}
What happens is, that we are ending with 2 unresolved externals. So, is the linker just an idiot? No, he is not. Thinking about what are unnamed namespace used for, we know what's going on. An unnamed namespace has an unique identifier in each translation unit. Thus, in our main.cpp, the linker doesn't know the definition of our private data in source.cpp.
So, what happens if we define this private data in main.cpp - just to bring matters to a head - ?
// main.cpp
#include "header0.hpp"
#include "header1.hpp"
std::string ns::header0::s_private = "header0 private data definition by main.cpp",
ns::header1::s_private = "header1 private data definition by main.cpp";
int main()
{
std::cout << "main.cpp:\n";
ns::header0_func0<int>();
ns::header0_func1<int>();
ns::header1_func0<int>();
ns::header1_func1<int>();
std::cout << '\n';
return 0;
}
Now, everything is compiling and getting linked 'correctly' - or rather, it seems so.
This is the console output of that program:
source.cpp:
header0_func0: header0 private data definition by source.cpp
header0_func1: header0 private data definition by source.cpp
header1_func0: header1 private data definition by source.cpp
header1_func0: header1 private data definition by source.cpp
main.cpp:
header0_func0: header0 private data definition by source.cpp
header0_func1: header0 private data definition by source.cpp
header1_func0: header1 private data definition by source.cpp
header1_func0: header1 private data definition by source.cpp
That means: If undefined behavior is what you are looking for, here you are.
In other words: Based on the explanation above: Don't use unnamed namespace in header files to encapsulate private shared data.
And the last question is, "what's the solution?"
If you don't want to use 'static' (utility) classes, you should prefer my first posted solution (only changed code):
// header0.hpp
// ...
namespace ns {
// ...
namespace detail {
class header0 { /*...*/ };
} /* namespace detail */
template<typename T> void header0_func0() { std::cout << "header0_func0: " << detail::header0::s_private << std::endl; }
template<typename T> void header0_func1() { std::cout << "header0_func1: " << detail::header0::s_private << std::endl; }
} /* namespace ns */
// ...
// header1.hpp
// ...
namespace ns {
// ...
namespace detail {
class header1 { /*...*/ };
} /* namespace detail */
template<typename T> void header0_func0() { std::cout << "header0_func0: " << detail::header1::s_private << std::endl; }
template<typename T> void header0_func1() { std::cout << "header0_func1: " << detail::header1::s_private << std::endl; }
} /* namespace ns */
// ...
// source.cpp
// ...
std::string ns::detail::header0::s_private = "header0 private data definition by source.cpp",
ns::detail::header1::s_private = "header1 private data definition by source.cpp";
// ...
I'm looking forward to any comment. Best regards.

What about that?
namespace foo {
namespace detail {
class shared
{
template<typename> friend void bar0();
template<typename> friend void bar1();
static const char* const m_private;
}; /* class shared */
} /* namespace detail */
template<typename T> void bar0() { std::cout << "bar0 said: " << detail::shared::m_private << std::endl; }
template<typename T> void bar1() { std::cout << "bar1 said: " << detail::shared::m_private << std::endl; }
} /* namespace foo */
EDIT 2:
OBSOLETE ANSWER. THIS CODE ISN'T WORKING! POSTED AN EXPLANATION BELOW.
EDIT:
In 'real code' I would replace namespace detail by an unnamed namespace. This would bring up the possibility to add other shared resources in different header files using the same name-scope:
namespace foo {
template<typename T> void bar0();
template<typename T> void bar1();
template<typename T> void bar2();
template<typename T> void bar3();
namespace {
class shared0
{
template<typename> friend void foo::bar0();
template<typename> friend void foo::bar1();
static const char* const m_private0;
}; /* class shared0 */
class shared1
{
template<typename> friend void foo::bar2();
template<typename> friend void foo::bar3();
static const char* const m_private1;
}; /* class shared1 */
} /* unnamed */
template<typename T> void bar0() { std::cout << "bar0 said: " << shared0::m_private0 << std::endl; }
template<typename T> void bar1() { std::cout << "bar1 said: " << shared0::m_private0 << std::endl; }
template<typename T> void bar2() { std::cout << "bar0 said: " << shared1::m_private1 << std::endl; }
template<typename T> void bar3() { std::cout << "bar1 said: " << shared1::m_private1 << std::endl; }
} /* namespace foo */

Related

Collect instantiated template types at compile time

Consider this snippet:
struct A {
template <typename T> void bar(const T &) {
/*
I would like to write something like:
if constexpr(type T already processed/T is already present in typelist)
...do something fancy
else if constexpr(type T not processed/T is not present in typelist)
*/
}
};
struct Msg1 {};
struct Msg2 {};
int main() {
A a;
a.bar(Msg1{});
a.bar(Msg1{});
}
Demo
Is it possible to see at compile time for which types the method bar was already instantiated?
Ideally, there would be some kind of growing typelist, where one can check at compile time for which types bar is already instantiated.
No. It is not possible to do so at compile time. However, it would be possible to do the following at runtime:
#include <typeindex>
#include <type_traits>
#include <unordered_set>
struct A {
private:
std::unordered_set<std::type_index> already_processed_ts;
public:
template<typename T>
void bar(const T&){
if(already_processed_ts.find(typeid(T)) != already_processed_ts.end())
std::cout << "Already processed " << typeid(T).name() << std::endl;
else{
already_processed_ts.insert(typeid(T));
std::cout << "Processing " << typeid(T).name() << "... \n";
}
}
}
struct Msg{};
int main()
{
f(Msg{}); // Prints "Processing 3Msg..." (at least on my compiler)
f(Msg{}); // Prints "Already processed 3Msg"
return 0;
}

Identifier not found error when accessing Template Class Member from Sub-Class

This is my very first question on StackOverflow. I've searched for similar "Identifier not Found" errors but couldn't find anything helpful. I'll be grateful for your help since I've been trying to work this out for 2 hours with no results.
It basically creates an object of type "Addition" and prints "1 + 1 = 2" (if it worked).
When I was not using templates it was working fine. But when I changed everything to templates it stopped working. I couldn't figure this out.
Visual studio does not underline anything in red, however I get a "identifier not defined" error for m_Argument_1 and m_Argument_2.
Thank you for your help and I apologize if this is a too simple question to ask here, but nevertheless I couldn't find a way out or figure out where the problem is.
This is my header file "Expression.h":
#ifndef OPERATION_HEADER
#define OPERATION_HEADER
#include <string>
#include <iostream>
template <typename T>
class Expression
{
protected:
char m_ExpressionSign; /* Represents Expression's Sign ('+','-','*','/') */
T m_Argument_1; /* Represents L Argument */
T m_Argument_2; /* Represents R Argument */
public:
Expression(T arg_1, T arg_2, char expressionSign) : m_Argument_1(arg_1), m_Argument_2(arg_2), m_ExpressionSign(expressionSign) { };
virtual T Result() const = 0;
virtual void Print(std::ostream& out) const;
friend std::ostream& operator << (std::ostream& out, const Expression<T>& expression);
};
template <typename T>
class Addition : public Expression<T>
{
public:
Addition(T arg1, T arg2) : Expression<T>(arg1, arg2, '+') {};
T Result() const { return m_Argument_1 + m_Argument_2; };
};
template <typename T>
void Expression<T>::Print(std::ostream& out) const
{
out << m_Argument_1 << " " << m_ExpressionSign << " " << m_Argument_2 << " = ";
}
template <typename T>
std::ostream& operator << (std::ostream& out, const Expression<T>& expression)
{
expression.Print(out);
return out;
}
#endif /* OPERATION_HEADER */
And this is my main.cpp:
#include <iostream>
#include <iomanip>
#include "Expression.h"
int main(int argc, char** argv)
{
Addition<int> A2(1, 1);
std::cout << "A2: " << A2 << " = " << std::setw(2) << A2.Result() << std::endl;
retun 0;
}
When inheriting from a class template, identifiers need to be qualified so that the compiler knows where they come from. In this case, in the definition of Addition, you could do:
T Result() const { return this->m_Argument_1
+ this->m_Argument_2; };
Alternatively, you could qualify the names like this:
T Result() const { return Expression<T>::m_Argument_1
+ Expression<T>::m_Argument_2; };

Function specializations in a class Template c++03/c++11 performance

I have two template classes and normally I would prefer the foo version of the Search class. But sadly my compiler can not understand the foo version because of the missing c++11 support. (He will not understand this line: template <typename U = _Format>)
The bar version will create an instance of the _Format type to call the right delegate. So also a constructor is called! Would this be a performance issue compared to the Foo version? (I know this would be this is like premature optimization, but I'm interested)
#include <iostream>
struct format1 {
format1(void) { }
};
struct format2 {
format2(void) { }
};
namespace foo
{
template <typename _Format>
class Search
{
public:
void createData()
{
doCreateData();
}
private:
template <typename U = _Format>
void doCreateData();
template <>
void doCreateData<format1>()
{
using namespace std;
cout << "Format1" << endl;
}
template <>
void doCreateData<format2>()
{
using namespace std;
cout << "Format2" << endl;
}
};
}
namespace bar
{
template <typename _Format>
class Search
{
public:
void createData(void)
{
doCreateData(_Format());
}
private:
void doCreateData(format1)
{
using namespace std;
cout << "Format1" << endl;
}
void doCreateData(format2)
{
using namespace std;
cout << "Format2" << endl;
}
};
}
int main(int argc, char *argv[])
{
(void)argc; (void)argv;
bar::Search<format2> search;
search.createData();
}

Separate Compilation of template with non-type argument

I'm trying to do separate compilation of my non-type argument template, but I am facing a few problems. My application has a number of opcodes, and each opcode is associated with certain data and functions, many of which are similar and could be defined in the primary template, then the differences can be put into specialized templates.
So here is the original in-line code:
(header)
#include <iostream>
template<int opcode>
class BAR
{
public:
BAR(){};
void foo()
{
std::cout << "BAR<int>::foo()" << std::endl;
}
};
// specialize 1 and 2, otherwise we go with the
// primary definition.
template <>
void BAR<1>::foo()
{
std::cout << "BAR<1>::foo()" << std::endl;
}
template <>
void BAR<2>::foo()
{
std::cout << "BAR<2>::foo()" << std::endl;
}
I have a simple 'main':
int main(int argc, char* argv[])
{
BAR<1> bar_1;
BAR<2> bar_2;
BAR<3> bar_3;
bar_1.foo();
bar_2.foo();
bar_3.foo();
return 0;
}
I managed to put the specializations for '1' and '2' into a cpp file and this is
what I have:
(header)
#include <iostream>
template<int opcode>
class BAR
{
public:
BAR(){};
void foo()
{
std::cout << "BAR<int>::foo()" << std::endl;
}
};
// specialize 1 and 2, otherwise we go with the
// primary definition.
template<> void BAR<1>::foo() ;
template<> void BAR<2>::foo() ;
(cpp)
#include "Foo.h"
#include <iostream>
template<> void BAR<1>::foo()
{
std::cout << "BAR<1>::foo()" << std::endl;
}
template<> void BAR<2>::foo()
{
std::cout << "BAR<2>::foo()" << std::endl;
}
void x()
{
BAR<1> b1;
BAR<2> b2;
b1.foo();
b2.foo();
}
I not real crazy about the x() function, but without this I get unresolved symbols for BAR<1>::foo() & BAR<2>::foo(), if there's a better way, I'd be interested ( MSVC 13 compiler).
Ok so far so good. I really want to put the primary definition of the function foo() into the CPP, but I can't seem to get the syntax right:
template<> void BAR<int>::foo()
{
std::cout << "BAR<int>::foo()" << std::endl;
}
this is not allowed by the compiler, and rightly so I guess, is not
a valid non-type value. So what is the magic words to use to do this?
Many thanks in advance for any help.
dave.

Why does my code throw "does not name a type error"?

driver.cc
#include <iostream>
#include "dynStack.h"
using namespace std;
// class definition
int main()
{
dynstack<double> it;
cout << "hello";
return 0;
}
dynStack.h
template <class T>
class dynstack {
public:
dynstack();
void push(T data);
private:
};
#include "dynStack.cc"
dynStack.cc
template <class T>
dynstack<T>::dynstack() { // <-- Here
}
template <class T> // // <-- And here
void dynstack<T>::push(T data)
{
}
I'm new to C++. The bolded lines are reported errors. The first one says "error: 'dynStack' does not name a type" The second one says "exrror: expected initializer before '<' token". I have spent hours on this and can't find the errors. Can anyone help? Thank you.
I was given a sample solution similar to this. Here is the sample:
main.cc
#include <iostream>
// #include the header file - as always
#include "temp.h"
using namespace std;
int main()
{
Thing<int> it(1);
Thing<double> dt(3.14);
cout << endl;
cout << "it = " << it.getData() << endl;
cout << endl;
cout << "dt = " << dt.getData() << endl;
cout << endl;
return 0;
}
temp.h
template <class T>
class Thing
{
private:
T data;
void setData(T data);
public:
Thing(T data);
T getData() const;
};
// MUST #include the implementation file here
#include "temp.cc"
temp.cc
// DO NOT #include the header file
template <class T>
Thing<T>::Thing(T data)
{
this->setData(data);
}
template <class T>
void Thing<T>::setData(T data)
{
this->data = data;
}
template <class T>
T Thing<T>::getData() const
{
return this->data;
}
It appears that you are trying to compile both driver.cc and dynStack.cc. The only file you compile with this setup is driver.cc.
Try this: Move contents of dynstack.cc entirely to dynstack.h and get rid of dynstack.cc
EDIT after reading comment responses:
If you want to keep dynstack.cc, its fine, just make sure you do not attempt to compile dynstack.cc I would name it a some other extension other than .cc which is conventionally for C++ implementation. Avoid .cc, .cpp, .cxx etc; use a uncommon extension such as .hc :-)