Can I call a non-member static templated function from a static member function where the definition is split into header and cpp:
// zero.cpp
class Zero
{
stat开发者_运维问答ic void zero() { one(5); }
};
// one.h
template <typename T>
static void one(T& var);
// one.cpp
template <typename T>
void one(T& var) { }
// main.cpp
...
Zero::zero()
...
I'm having problems getting this to link, I keep getting undefined reference to the function I'm trying to define in one.cpp.
Initially I thought it was due to a problem with namespacing, but all files are now in the same namespace. Am I doing anything fundamentally wrong here?
Template definitions need to be visible at the point of instantiation. That is, it needs to be in the header somehow:
// one.hpp
template <typename T>
static void one(T& var)
{
// definition visible in header
}
Though I'm not sure why you'd want it to be static.
As an addition to GMan's answer I would like to note that you can't make T&
bind to an rvalue such as the integral literal 5
which is of type int
. 5 will not bind to int&
, but will bind to const int&
.
精彩评论