English 中文(简体)
C++ 部分模板专业化
原标题:C++ partial template specialization

,这一回答是 认为“良好”守则,还是仅仅是一个ug?

我也想知道,这是如何预先宣布的(这两类)。

当我刚才用2个模板参数宣布这一类别时,它就总是这样做,不管其价值是什么<代码>flag。

I would like to do this because I have 2 special member functions which should behave differently on flag being true and I don t feel like reimplementing the whole class. Also, it should have the same name. According to this example, this seems to be possible.

And I have to forward-declare it because I m creating a library in which we forward-declare everything.

任何想法?

最佳回答

为了使用特殊化,其定义必须始终为打电话者所熟知。 举例来说,如果您有<条码>template <打字型号、 b旗和gt号;在座标和<条码>中界定的构件:template <打字形形;真的“Type”; :公共东西加固;Type,假装;在第二版中定义,使用后者,以包括第二位标题。 如果没有,你将永远获得第一、更通用的类型。

EDIT:关于前宣讲的比喻是我的想法。 如果你只想像点变量那样使用类型声明,那么:

标题

template <class Type, bool flag>
struct something;

struct Test
{
    something<int, true>* ptr; // definition not needed
    Test();
}

来源

#include "something.h" // header with template
#include "something_spec.h" // header with specialisation

Test::Test()
{
    ptr = new something<int, true>(); // specialisation used
}
问题回答

它的缺点是它实际上没有工作。 基地成员的职能不是压倒一切的,但当你试图从外部打电话时,这种职能就被衍生的阶级职能所掩盖。 如果您在基数类别(在假定你所有其他职能都有效)中打电话到<代码>doSomething,则该代码将称为基数类别:,但不是想要的。

在这个问题上得到接受的回答表明,如何解决你的问题有多种途径。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签