I have such test.hpp:
#include <cstring>
#include <cassert>
#include <map>
template <typename T, typename Tag>
struct Boo {
using ElementType = T;
static const char name[];
};
struct CFG_ELM_NAME__Pref1 {};
using Pref1 = Boo<int, CFG_ELM_NAME__Pref1>;
struct Foo {
template <typename CfgElm>
void get() const {
auto it = cache_.find(CfgElm::name);
assert(it != cache_.end());
}
Foo();
private:
struct CmpCStr final {
bool operator()(const char *a, const char *b) const {
return std::strcmp(a, b) < 0;
}
};
using PrefCacheMap = std::map<const char *, int, CmpCStr>;
PrefCacheMap cache_;
};
and use it like this (test2.cpp):
#include "test.hpp"
void f()
{
Foo foo;
foo.get<Pref1>();
}
and initialize it like this (test.cpp):
#include "test.hpp"
template <> const char Pref1::name[] = "Pref1";
Foo::Foo()
{
cache_.insert(std::make_pair(Pref1::name, 17));
}
This is reduced example, so Foo::get do nothing.
clang produces such warning for this code:
clang++ -Wall -Wextra -std=c++11 test.cpp test2.cpp
In file included from test2.cpp:1:
./test.hpp:15:35: warning: instantiation of variable 'Boo<int,
CFG_ELM_NAME__Pref1>::name' required here, but no definition is
available [-Wundefined-var-template]
auto it = cache_.find(CfgElm::name);
^
test2.cpp:6:6: note: in instantiation of function template specialization
'Foo::get<Boo<int, CFG_ELM_NAME__Pref1> >' requested here
foo.get<Pref1>();
^
./test.hpp:7:21: note: forward declaration of template entity is here
static const char name[];
^
./test.hpp:15:35: note: add an explicit instantiation declaration to
suppress this warning if 'Boo<int, CFG_ELM_NAME__Pref1>::name' is
explicitly instantiated in another translation unit
auto it = cache_.find(CfgElm::name);
This codes does compiles and links without problems.
The only problem is warning.
I have no idea how to suppress it.
I found this question explicit instantiation of static variable of a template class in different translation units , but I can not use provided solution, because of I don't know template arguments.
I can not write: template<> int Boo<Type1, Type2>::name;
because of the whole idea is to use my code like this: foo.get<Pref1>(), without explicitly pointing that Pref1 is Boo<int, CFG_ELM_NAME__Pref1>.
So anybody know how to suppress warning, without turn of such warning for whole project via -Wno-undefined-var-template?
You should add Boo::name template definition in the same header file:
static const char * name;
}; // End Boo
template<typename T, typename Tag>
const char * Boo<T, Tag>::name{};
Update: now it is clarified that you are trying to write specialization for name in some translation unit after instantiating it in header file. This will require some trickery. You need to declare specialization in header file before instantiating it and probably use external template and explicitly instantiate it in the same translation unit as name:
// header
template <typename T, typename Tag> struct Boo {
using ElementType = T;
static const char * name;
};
struct CFG_ELM_NAME__Pref1 {};
// indicate that name for this specialization exists elsewhere
template<> const char * Boo<int, CFG_ELM_NAME__Pref1>::name;
// indicate that template is defined somewhere
extern template struct Boo<int, CFG_ELM_NAME__Pref1>;
using Pref1 = Boo<int, CFG_ELM_NAME__Pref1>;
// test.cpp
// definition will be present only in this translation unit
template<> const char * Boo<int, CFG_ELM_NAME__Pref1>::name{"Pref1"};
// explicit instantiation
template struct Boo<int, CFG_ELM_NAME__Pref1>;
online compiler
Related
I am trying to compile the below code, and I am getting the error as
"error: ‘MD5Sum’ is not a class template
template<> struct MD5Sum<::cv_bridge::CvImage>"
template<> struct MD5Sum<::cv_bridge::CvImage>
{
static const char* value() { return MD5Sum<::sensor_msgs::msg::Image>::value(); }
static const char* value(const ::cv_bridge::CvImage&) { return value(); }
static const uint64_t static_value1 = MD5Sum<::sensor_msgs::msg::Image>::static_value1;
static const uint64_t static_value2 = MD5Sum<::sensor_msgs::msg::Image>::static_value2;
// If the definition of sensor_msgs/Image changes, we'll get a compile error here.
ROS_STATIC_ASSERT(MD5Sum<::sensor_msgs::msg::Image>::static_value1 == 0x060021388200f6f0ULL);
ROS_STATIC_ASSERT(MD5Sum<::sensor_msgs::msg::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
};
This seems to be template specialization. Does it means that template class should be present first before make template specialization out of it.
The class template MD5Sum needs to be declared before any of its specializations.
You either need to include the file where the template is declared, or declare it yourself.
You can use an empty definition (if you only want to call fully specialized versions) or the generic implementation you choose:
// add this before your specialization
template <class T> struct MD5Sum; // empty declaration
// or your default implementation
template <class T> struct MD5Sum {
...
};
The sample code compiles without warning or error on Windows and Linux. It starts to get the -Wundefined-var-template warning in XCode 9.
foo.h:
template <typename T>
struct myClass
{
static const char* name;
};
foo.cpp:
#include "foo.h"
template<>
const char *myClass<int>::name = "int";
warning: instantiation of variable 'myClass<int>::name' required here, but no definition is available [-Wundefined-var-template]
note: forward declaration of template entity is here
static const char *name;
^
note: add an explicit instantiation declaration to suppress this warning if 'myClass<int>::name' is explicitly instantiated in another translation unit
What I tried but didn't work:
Moving the code in foo.cpp to foo.h resulted in "duplicate code" error at link-time, because foo.h was included by multiple files.
Adding the following code in the header file can resolve the warning, but the code fails at runtime.
template<T>
const char *myClass<T>::name = "";
What worked in the end:
foo.h:
template <typename T>
struct myClass
{
static const char* name;
};
template <>
struct myClass<int>
{
static const char* name;
};
foo.cpp:
#include "foo.h"
const char *myClass<int>::name = "int";
I wanted to check that the extern keyword did infact prevent class code being generated in the translation unit:
template<class> struct always_false : std::false_type {};
template <typename T> class A{
static_assert(always_false<T>::value, "If this fires, 'A' is instantiated");
};
extern template class A<int>; //error: static assertion failed: If this fires, 'A' is instantiated|
int main(){
// A<int> f;
}
Why is it that the previous code still produces an error from the static_assert if this is my only source file? As far as I understand from the explicit use of extern this should prevent any production of code for the class A<int> and the linker takes care of finding a later explicit instantiation definition (in the translation unit for which the code is actually written) to match any use of A<int> with.
However it seems that the explicit instantiation declaration is generating code in this translation unit itself as indicated by the compilation error. If I comment out extern template class A<int> everything works fine. I was using GCC 4.9.2. but it appears clang 3.5.1 throws this error too.
Alternatively this also kicks up the same assert error:
template<class> struct always_false : std::false_type {};
template <typename T> class A{
public:
void test() { static_assert(always_false<T>::value, "If this fires, 'test()' is instantiated"); }
};
extern template void A<int>::test();
int main(){
A<int> a;
a.test();
}
Here I would've expected the member function A<int>::test() to not even be instantiated and again wait until linking before "finding" code for the function, but it looks like the code is generated in the same translation unit. However if I take out the static_assert:
template <typename T> class A{
public:
void test() { }
};
extern template void A<int>::test();
int main(){
A<int> a;
a.test();
}
Then I get the error I'm expecting, indicating that A<int>::test() is not instantiated and there was a linker error:
**undefined reference to `A<int>::test()'|**
Why would the static_assert throw an error if test() was never instantiated?
Your premise is wrong. extern template prevents object code generation for function templates (including member functions of class templates), but it doesn't prevent the instantiation of the class bodies.
Edit: To answer the updated question: the member function is defined inline in the class, so the compiler will still instantiate it so that it can inline it if necessary. If you define the function out of line, you will not get an error (tried GCC 5.2.0 via godbolt).
#include <type_traits>
template<class> struct always_false : std::false_type {};
template <typename T> class A{
public:
void test();
};
template <typename T>
void A<T>::test() { static_assert(always_false<T>::value, "If this fires, 'test()' is instantiated"); }
extern template void A<int>::test();
int main(){
A<int> a;
a.test();
}
The problem I am struggling with is the declaration of specialized template function inside template class (I keep class declaration in header file and define member functions in associated .C file).
I have template class representing Points. The header file is presented below:
//...
template<typename T, int dim=3> // T - coords. type, int dim - no. of dimensions
class Point {
public:
// ...
// function below sets val at the given position in array m_c and returns reference
template<int position> Point& set(T val);
private:
T m_c[dim]; // coordinates
};
//...
definition of function set is placed in .C file:
template<typename T, int dim> template<int position> Point<T, dim>& Point<T, dim>::set(T val){
// ...
return *this;
}
As I understand this is the most general form of its definition.
In main function I create Point with float as T and try to set some values in the array:
int main(int argc, char** argv) {
Point<float> p1;
p1.set<0>(3).set<1>(3.6).set<2>(3);
//...
}
In order to make this possible with definition of the member functions of template outside header file I need to inform compiler about specialization in .C file:
template class Point<float>;
and I need as well to declare usage of set function, which I try to accomplish this way
(and this piece of code is the problem):
template<> template<int> Point<float>& Point<float>::set(float);
That unfortunately doesn't do the job and I get errors:
/tmp/ccR7haA5.o: In function `main':
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<0>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<1>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<2>(float)'
I would really appreciate an explanation from someone who may know how to cope with this problem. Thanks.
In order to provide the definition of a function template specialization in a different TU, you need an explicit instantiation declaration:
[Point.hpp]
template<typename T, int dim=3>
struct Point
{
template<int position> Point& set(T val);
};
// `extern` makes this an explicit instantiation _declaration_
extern template Point<float,3>& Point<float,3>::set<0>(float);
extern template Point<float,3>& Point<float,3>::set<1>(float);
extern template Point<float,3>& Point<float,3>::set<2>(float);
[Point.cpp]
#include <iostream>
#include "Point.hpp"
template<typename T, int dim>
template<int position>
Point<T,dim>& Point<T,dim>::set(T val)
{
// note: non-standard macro
std::cout << __PRETTY_FUNCTION__ << std::endl;
return *this;
}
// no `extern`: this is an explicit instantiation _definition_
// which instantiates the function template, and therefore requires the definition
// to be available in this TU
template Point<float,3>& Point<float,3>::set<0>(float);
template Point<float,3>& Point<float,3>::set<1>(float);
template Point<float,3>& Point<float,3>::set<2>(float);
[main.cpp]
#include "Point.hpp"
int main()
{
// in this TU, there's no definition for the function template
// hence, it cannot be instantiated
// however, we can use the explicit instantiations
Point<float,3>().set<0>(0);
Point<float,3>().set<1>(0);
Point<float,3>().set<2>(0);
// does not compile (linker error):
//Point<int,3>().set<0>(0);
//Point<float,4>().set<0>(0);
//Point<float,3>().set<4>(0);
}
When I try to compile this with Clang
template<class T>
struct Field
{
char const *name;
Field(char const *name) : name(name) { }
};
template<class Derived>
class CRTP { static Field<Derived> const _field; };
class Class : public CRTP<Class> { };
Field<Class> const CRTP<Class>::_field("blah");
int main() { }
I get
error: template specialization requires 'template<>'
Field<Class> const CRTP<Class>::_field("blah");
~~~~~~~~~~~ ^
I don't understand the error at all. What is wrong with my definition of _field and how do I fix it?
(Note that the arguments to _field are not necessarily the same for all subclasses.)
For the compiler to identify this as a template specialization (e.g. to be able to check the syntax), you need the template keyword:
template<>
Field<Class> const CRTP<Class>::_field("blah");
Its brackets are empty as all template parameters are specialized, but you cannot just leave it away.
The error says exactly what is missing. template<> is missing before that line.
template<>
Field<Class> const CRTP<Class>::_field("blah");
Note, however, that your typing of Field<Class>, if unique, could be used to construct all instances of Field<Class> with a given string.
template<typename T>
struct field_trait;
template<class T>
struct Field
{
char const *name;
Field() : name(field_trait<T>::get_name()) {}
};
template<class Derived>
class CRTP { static Field<Derived> const _field; };
template<class Derived>
class CRTP<Derived>::_field;
class Class;
template<>
struct field_traits<Class> {
static const char* get_name() { return "blah"; }
};
class Class : public CRTP<Class> { };
int main() { }
which means that every instance of Field<Class> always has the name "blah".
One question I would have is, do you really need storage for said Field<Class> to actually have a pointer to a string, and if so does it need to be unique, and if so does it need to be "bare"? Because figuring out where the static instance exists is somewhat annoying.
Together with field_traits above:
template<class Derived>
class CRTP { static Field<Derived>& field() const { static Field<Derived> _field( field_traits<Derived>::get_name()); return _field; };
this moves the problem of "where is the _field stored" to being the compilers problem. And it is initialized by the contents of field_traits<T>::get_name().
A static data member must have both a declaration and a definition. If this was a plain class it would look like this:
// header:
class C {
static int i;
};
// source:
int C::i = 3;
Templates aren't ordinarily defined in source files, so the code would look something like this:
// header:
template <class T>
class C {
static int i;
};
template <class T>
int C<T>::i = 3;
In your code, you don't have the definition of the static data member. That's okay if you don't use it. But the code that the compiler is complaining about defines a static data member for CRTP<Class>; that's a specialization (because it's not applicable to all instantiations of CRTP, just to this one), and the compiler is saying that you have to tell it that it's a specialization. So do as you're told:
template <>
Field<Class> const CRTP<Class>::_field("blah");
or, to write the non-specialized template version, use the usual template syntax:
template <class T>
Field<T> const CRTP<T>::_field("blah");