1

如果我做:

(gdb) b nmspace::TestClass::compareFOO

然后将以下方法签名作为断点进行签名:

nmspace::TestClass::compareFOO(blah::Foo const&, blah::Foo const&, unsigned int)
nmspace::TestClass::compareFOO(blah::Foo const&, blah::FooField const&, unsigned int)
nmspace::TestClass::compareFOO(blah::FooField const&, blah::Foo const&, unsigned int)
nmspace::TestClass::compareFOO(blah::FooField const&, blah::FooField const&, unsigned int)

是否有类似以下的内容,或者我们必须每次都为模板方法编写四个?多态性不是也适用于 C++ 模板吗?

nmspace::TestClass::compareFOOES<blah::Foo, blah::Foo>
nmspace::TestClass::compareFOOES<blah::Foo, blah::FooField>
nmspace::TestClass::compareFOOES<blah::FooField, blah::Foo>
nmspace::TestClass::compareFOOES<blah::FooField, blah::FooField>

我试过nmspace::TestClass::compareFOOES, nmspace::TestClass::compareFOOES*,nmspace::TestClass::compareFOOES<>()

4

1 回答 1

1

如果所有函数的主体都相同,您可以编写一个全局函数,该函数可以使用std::enable_ifor接受多种类型的参数boost::enable_if

template< class T >
struct is_valid_field
    : boost::or_<boost::is_same<T, blah::Foo>, boost::is_same<T, blah::FooField>>
{
};

template< class T, class Q >
nmspace::TestClass::compareFOO(T const&, Q const&, unsigned int,
    typename boost::enable_if<
        boost::and_<is_valid_field<T>, is_valid_field<Q>
    >::type* = 0)
{
    // implementation
}
于 2012-12-21T19:19:54.030 回答