开发者

Forcing template function instantiation in class

开发者 https://www.devze.com 2023-04-01 03:12 出处:网络
I have a function that is declared like: template<typename T> void MyFunction(); And a class: template<typename T>

I have a function that is declared like:

template<typename T>
void MyFunction();

And a class:

template<typename T>
class MyClass
{
public:

    typedef void (*Function_T)();
    开发者_开发百科Function_T m_Func;
    void DoSomething()
    {
        m_Func = &MyFunction<T>;
    }
}

When I use the class, I undefined symbols error on MyFunction<T>.

If I change DoSomething to

void DoSomething()
{
    m_Func = &MyFunction<T>;
    return;
    MyFunction<T>();
}

Everything works, but that looks like a workaround and will probably not work with optimization.

I cannot add

template void MyFunction<T>;

to the class because it says it cannot be in class. Is there any other way I can force instantiation of the function?

Edit:

I was able to write a test that fails, but in g++ it has a different message and actually a compiler error: http://ideone.com/RbMnh


Your code will work with optimization as well. Although, I don't know why simply m_Func = &MyFunction<T> doesn't work. GCC 4.3.4 compiles it fine. Which compiler you're using?

And you can also do this:

void DoSomething()
{
    if ( false) MyFunction<T>();
    m_Func = &MyFunction<T>;
    return;
}

By the way, the function pointer type is incorrectly defined. It should be this:

typedef void (*Function_T)();
                     //   ^^ put this!


Your code compiles fine for me using GCC, so I'm not sure if this solution solves your particular problem, but you can explicitly instantiate template functions like so:

// Template function defined:
template <typename T>
void MyFunction() {
    // body
}

// Template function instantiated:
template void MyFunction<int>();


The issue can be either a compiler bug, or an error in the parts of the code that you are not showing. Try to build a minimal example that reproduces the problem, this is the minimal example that I have been able to produce, and compiles fine with both clang++ 2.8 and g++ 4.4/4.5:

drodriguez@drodriguez-desktop:/tmp$ cat test.cpp 
#include <iostream>

template <typename T>
void function() {
}

template <typename T>
struct type {
    typedef void (*Func)();
    Func _f;
    void f() {
        _f = &function<T>;
    }
};

int main() {
    type<int> t;
    t.f();
    std::cout << t._f << std::endl;
}
drodriguez@drodriguez-desktop:/tmp$ /usr/bin/clang++ --version
clang version 2.8 (branches/release_28)
Target: x86_64-pc-linux-gnu
Thread model: posix
drodriguez@drodriguez-desktop:/tmp$ /usr/bin/clang++ -o test test.cpp && ./test
1
drodriguez@drodriguez-desktop:/tmp$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

drodriguez@drodriguez-desktop:/tmp$ g++-4.4 -o test test.cpp && ./test
1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号