我正在尝试制作一种可以类型安全地封装任意类型的类型。我从这个答案中得到了这样的想法:5 年后,有没有比“最快的 C++ 代表”更好的东西?到目前为止,我只成功地解决了这个问题,但是我遇到了一个我找不到根源的错误。
编译器似乎告诉我它不能将值转换为值自己的类型,这让我觉得很奇怪。
我正在运行带有 llvm-gcc 4.2(gcc 4.2.1 前端)的 Mac OS X 10.6。
欢迎提出如何摆脱 void* 或将其移至不太重要的位置的建议,但这个问题并不是真正的问题。
错误:
$ g++ main.cpp
main.cpp: In static member function ‘static Stamp StampFactory<T>::make(T*) [with T = int]’:
main.cpp:33:   instantiated from ‘Stamp makeStamp(T*) [with T = int]’
main.cpp:39:   instantiated from here
main.cpp:26: error: could not convert template argument ‘t’ to ‘int*’
编码:
typedef void (*VoidFunc)(void*);
struct Stamp
{
    Stamp(VoidFunc p)
    {
        this->press = p;
    }
    VoidFunc press;
};
template<typename T>
struct StampFactory
{
    template<T* rvalue>
    struct Pattern
    {
        void operator()(void* lvalue)
        {
            *dynamic_cast<T*>(lvalue) = *rvalue;
        }
    };
    static Stamp make(T* t)
    {
        return Stamp(Pattern<t>()); // 28
    }
};
template<typename T>
Stamp makeStamp(T* t)
{
    return StampFactory<T>::make(t); // 33
}
int main(int argc, char** argv)
{
    int i = 0;
    Stamp s = makeStamp(&i); //39
}