我在当前项目中使用包含虚函数的类模板,我偶然发现了一个我无法独自克服的问题。
- 类模板的成员函数体不能从类中分离出来 由于链接器错误而在 .hpp 文件中定义。我不想 为我将要使用的每个新类型实例化我的模板,所以所有 剩下的就是让它们内联。这绝对是 很好,因为它们大部分时间都是 1-2 行,所以我不会去 体验任何代码膨胀。
- 另一方面,gcc 为多态类创建 vtable 具有第一个非内联函数定义的 .cpp 文件 在类定义中声明。因为我有所有成员 内联函数,我得到对 vtable 的 undefined reference ,或者没有 在 GDB 中为我的类(class)找到 RTTI 符号。
请考虑以下代码:
template <typename T>
struct Test
{
virtual void testMe() const = 0;
virtual ~Test() = default;
};
template <typename T>
struct test : public Test<T>
{
virtual void testMe() const
{
std::cout << typeid(T).name() << std::endl;
}
virtual ~test() = default;
};
int main()
{
test<int> t;
Test<int>& T = t;
T.testMe();
return 0;
}
在这个特定的例子中,我得到:
can't find linker symbol for virtual table for `test<int>' value
使用 GDB 调试时。
当所有类函数都是内联时,如何强制我的编译器将 vtable 放入特定的 cpp 文件中?
编辑:
由于上面提供的例子没有说明问题,这里是我的原始代码。
导致问题的类:
#ifndef CONVERTIBLETO_H
#define CONVERTIBLETO_H
#include "convertibleTo_converters.h"
#include <functional>
template <
typename IT,
template <typename InterfaceType, typename ErasedType>
class Converter = convertibleTo_detail::default_converter
>
class convertibleTo
{
public:
typedef convertibleTo<IT, Converter> this_type;
typedef IT InterfaceType;
struct is_type_eraser_tag {};
private:
class holder_interface
{
public:
virtual InterfaceType get() const = 0;
virtual void set(const InterfaceType&) = 0;
virtual holder_interface* clone() const = 0;
virtual ~holder_interface() {}
};
template <typename ErasedType>
class holder : public holder_interface
{
public:
virtual InterfaceType get() const
{
return (Converter<InterfaceType, ErasedType>::convert(this->data));
}
virtual void set(const InterfaceType& value)
{
this->data = (Converter<InterfaceType, ErasedType>::convert(value));
}
virtual holder_interface* clone() const
{
return new holder(*this);
}
holder() = delete;
holder(const holder& other):
data(other.data)
{ }
holder(ErasedType& d):
data(d)
{ }
virtual ~holder() = default;
private:
ErasedType& data;
};
public:
inline InterfaceType get() const
{
if (this->held)
return this->held->get();
else
return InterfaceType();
}
inline void set(const InterfaceType& value)
{
if (this->held)
this->held->set(value);
}
inline bool empty() const
{
return ! this->held;
}
convertibleTo<InterfaceType, Converter>& operator= (const convertibleTo<InterfaceType, Converter>& other)
{
if(this->held)
delete this->held;
this->held = other.held->clone();
return *this;
}
convertibleTo():
held(nullptr)
{ }
template <typename T>
explicit convertibleTo(T& data):
held(new holder<T>(data))
{
}
convertibleTo( convertibleTo& other ):
convertibleTo( const_cast<const convertibleTo&>(other))
{
}
convertibleTo( const convertibleTo& other ):
held(nullptr)
{
if(other.held)
this->held = other.held->clone();
}
~convertibleTo()
{
if (this->held)
delete this->held;
}
private:
holder_interface * held;
};
#endif
必需的辅助类:
#ifndef CONVERTIBLETO_CONVERTERS_H
#define CONVERTIBLETO_CONVERTERS_H
#include <string>
#include <sstream>
namespace convertibleTo_detail
{
template <typename InterfaceType, typename ErasedType>
struct default_converter
{
static inline InterfaceType convert(const ErasedType& input)
{
return input;
}
static inline ErasedType convert(const InterfaceType& input)
{
return input;
}
};
template <typename T>
struct default_converter<T, T>
{
static inline T convert(const T& input)
{
return input;
}
};
template <typename ErasedType>
struct default_converter<std::string, ErasedType>
{
static inline std::string convert(const ErasedType& input)
{
default_converter<std::string, ErasedType>::prepareConverter();
default_converter<std::string, ErasedType>::converter << input;
return default_converter<std::string, ErasedType>::converter.str();
}
static inline ErasedType convert(const std::string& input)
{
default_converter<std::string, ErasedType>::prepareConverter(input);
ErasedType result;
default_converter<std::string, ErasedType>::converter >> result;
return result;
}
private:
static std::stringstream converter;
struct SetExceptionFlagsOnce
{
SetExceptionFlagsOnce()
{
default_converter<std::string, ErasedType>::converter.exceptions(std::stringstream::failbit);
}
};
static void inline prepareConverter(std::string value = "")
{
static SetExceptionFlagsOnce setter;
default_converter<std::string, ErasedType>::converter.clear();
default_converter<std::string, ErasedType>::converter.str(value);
}
};
template <typename ErasedType>
std::stringstream default_converter<std::string, ErasedType>::converter;
template <>
struct default_converter<std::string, std::string>
{
static inline std::string convert(const std::string& input)
{
return input;
}
};
}
#endif // CONVERTIBLETO_CONVERTERS_H
主要.cpp:
#include <iostream>
#include "convertibleTo.h"
int main()
{
int I = 5;
convertibleTo< std::string > i(I);
std::cout << i.get() << std::endl;
i.set("321");
std::cout << i.get() << std::endl;
return 0;
}
我得到的错误是:
RTTI symbol not found for class 'convertibleTo<std::string, convertibleTo_detail::default_converter>::holder<int>'
当我进入 i.get(),然后进入 holder 的 get() 时显示。
编辑:根据建议将完整源代码从 pastebin 移至此处
既然最后两条评论说这是一个 GDB bug,下次我该如何自己检查呢?
- 如果 GDB 提示缺少 vtable - 确认我可以通过对使用派生类初始化的 ABC 的引用访问每个虚拟成员是否足以确认一切正常?
- 如果 GDB 提示缺少 RTTI 符号 - 在对使用派生类初始化的 ABC 的引用上调用 typeid() 是否足以确认 RTTI 符号实际上存在?
最佳答案
你的代码(完整版,有两个头文件和 main.C)编译和链接对我来说没有任何错误,使用 gcc 4.8.3,默认选项(除了 -std=c++11,到启用 C++11 模式)。
我什至将生成的可执行文件加载到 gdb 中。 gdb 毫无问题地吞下了它。
我看不出有什么问题。
关于c++ - 内联所有虚函数的模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222092/