Generic Programming: Mappings between Types and Values

关于template标签用法总结(含vue中的用法总结) 文章目录html5中的template标签template标签操作的属性和方法vue中的template html5中的template标签 html中的template标签中的内容在页面中不会显示。但是在后台查看页面DOM结构存在template标签。这是因为template标签天生不可见,它设置了display:none;属性。 <!--当前页面只显示"我是自定义表现abc"这个内容,不... 阅读详情

Generic<Programming>: Mappings between Types and Values

Andrei Alexandrescu


前言

    Andrei Alexandrescu的文章不用我说了吧,经典!由于小弟水平有限,不敢翻译,恐有误人子弟之嫌,好了大家还是赶快看吧。^_^,望看过的朋友发表一下你的心得,共同探讨。

Note: This article updated September 21, 2000

The term "conversion" in C++ describes the process of obtaining a value of one type from a value of another type. However, sometimes you need a different kind of conversion: you might need to obtain a value when you have a type, or the other way around. Such conversions are unnatural to C++ because the world of types and that of values are separated by a thick boundary. In certain circumstances, however, you need to cross the border between the two, and this column discusses how to do that.

Mapping Integers to Types

A surprisingly simple template can be very helpful to many generic programming idioms:

template <int v>
        

struct Int2Type
        

{
        

    enum { value = v };
        

};
        

Int2Type "generates" a distinct type for each distinct constant integral value passed. This is because different template instantiations are distinct types, so Int2Type<0> is different from Int2Type<1> and so on. In addition, the value that generated the type is "saved" in the enum member value.

You might use Int2Type whenever you need to quickly "typify" an integral constant. Say, for example, that you design a NiftyContainer class template.

template <typename T>
        

class NiftyContainer
        

{
        

    ...
        

};
        

NiftyContainer stores pointers to T. In some member functions of NiftyContainer, you need to clone objects of type T. If T is a non-polymorphic type, you would say:

T* pSomeObj = ...;
        

T* pNewObj = new T(*pSomeObj);
        

With polymorphic types, the story is a bit more complicated, so let's say you establish the convention that all polymorphic types to be used with NiftyContainer must define a Clone virtual function. You can then clone objects like so:

T* pNewObj = pSomeObj->Clone();
        

Because your container must be able to accommodate both types, you must implement both cloning algorithms and select the appropriate one at compile time. You then communicate whether or not a type is polymorphic through a boolean (non-type) template parameter of NiftyContainer and rely on the programmer to pass the correct flag.

template <typename T, bool isPolymorphicWithClone>
        

class NiftyContainer
        

{
        

    ...
        

};
        

 
        

NiftyContainer<Widget, true> widgetBag;
        

NiftyContainer<double, false> numberBag;
        

If the type you store in NiftyContainer is not polymorphic, you can optimize many member functions of NiftyContainer because you can count on constant object size and value semantics. In all these member functions, you need to select one algorithm or another depending on the isPolymorphic template argument.

At first glance, it seems like you can use a mere if statement.

template <typename T, bool isPolymorphic>
        

class NiftyContainer
        

{
        

    ...
        

    void DoSomething(T* pObj)
        

    {
        

        if (isPolymorphic)
        

        {
        

            ... polymorphic algorithm ...
        

        }
        

        else
        

        {
        

            ... non-polymorphic algorithm ...
        

        }
        

    }
        

};
        

The problem is that the compiler won't let you get away with this code. For example, if the polymorphic algorithm uses pObj->Clone, then NiftyContainer::DoSomething does not compile for any type that doesn't define a member function Clone. True, it is obvious at compile time what branch of the if statement executes. However, that doesn't matter to the compiler, which diligently tries to compile both branches, even if the optimizer will later eliminate the dead code. If you try to call DoSomething for NiftyContainer<int, false>, the compiler stops at the pObj->Clone call and says, "huh?"

But wait, there's more. If T is a polymorphic type, again the code might not compile. Assume T disables its copy constructor by making it private or protected — as a well-behaved polymorphic class should do. Then, if the non-polymorphic code branch attempts new T(*pObj), the code fails to compile.

It would be nice if the compiler didn't bother with compiling dead code, but that's not the case, so what would be a satisfactory solution?

As it turns out, there are a number of solutions, and Int2Type provides a particularly clean one. It can transform ad-hoc the boolean value isPolymorphic into two distinct types corresponding to isPolymorphic's true and false values. Then you can use Int2Type<isPolymorphic> with simple overloading, and voilà!

The incarnation of the "integral typifying" idiom is shown below.

template <typename T, bool isPolymorphic>
        

class NiftyContainer
        

{
        

private:
        

    void DoSomething(T* pObj, Int2Type<true>)
        

    {
        

        ... polymorphic algorithm ...
        

    }
        

    void DoSomething(T* pObj, Int2Type<false>)
        

    {
        

        ... non-polymorphic algorithm ...
        

    }
        

public:
        

    void DoSomething(T* pObj)
        

    {
        

        DoSomething(pObj, Int2Type<isPolymorphic>());
        

    }
        

};
        

The code is simple and to the point. DoSomething calls a private overloaded function. Depending on the value of isPolymorphic, one of the two private overloads gets called, completing the dispatch. The dummy temporary variable of type Int2Type<isPolymorphic> isn't even used; it serves only for propagating type information.

Not So Fast, Skywalker!

Seeing the above, you might think there's a smarter way, by using some of those template specialization tricks. Why is that dummy temporary necessary? There's got to be a better way. Surprisingly, however, Int2Type is very hard to beat in simplicity, generality, and effectiveness.

One attempt could involve specializing NiftyContainer::DoSomething for any T and the two possible values of isPolymorphic. This is the charter of partial template specialization, right?

template <typename T>
        

void NiftyContainer<T, true>::DoSomething(T* pObj)
        

{
        

    ... polymorphic algorithm ...
        

}
        

 
        

template <typename T>
        

void NiftyContainer<T, false>::DoSomething(T* pObj)
        

{
        

    ... non-polymorphic algorithm ...
        

}
        

As nice as the code above might look, alas, it's not legal. There is no such thing as partially specializing one member function of a class template. You can partially specialize all of NiftyContainer:

template <typename T>
        

class NiftyContainer<T, false>
        

{
        

    ... non-polymorphic NiftyContainer ...
        

};
        

You also can totally specialize DoSomething:

template <>
        

void NiftyContainer<int, false>::DoSomething(int* pObj)
        

{
        

    ... non-polymorphic algorithm ...
        

}
        

but, oddly, you cannot do anything in between [1].

Another solution might involve using traits [2] and implementing DoSomething outside NiftyContainer (in the traits class). This can be awkward as it segregates part of DoSomething's implementation.

A third solution that attempts to still use traits, but tries to keep everything together, would be to define the traits class inside NiftyContainer as a private inner class. To make a long story short, this works, but by the time you manage to implement it, you come to realize just how much better the Int2Type-based idiom is. And maybe the nicest part of it is that you can actually put the little Int2Type template in a library and document its intended use.

Type-to-Type Mappings

Consider the function below:

template <class T, class U>
        

T* Create(const U& arg)
        

{
        

    return new T(arg);
        

}
        

Create makes a new object, passing an argument to its constructor.

Now say there is a rule in your application: objects of type Widget are legacy code and must take two arguments upon construction, the second being a fixed value like -1. You don't have this problem in all classes derived from Widget.

How can you specialize Create so that it treats Widget differently from all other types? You cannot partially specialize a function; that is, you can't say something like:

// Illegal code - don't try this at home
        

template <class U>
        

Widget* Create<Widget, U>(const U& arg)
        

{
        

    return new Widget(arg, -1);
        

}
        

In absence of partial specialization of functions, the only tool we have is, again, overloading. We might pass a dummy object of type T and rely on overloading.

// An implementation of Create relying on overloading
        

template <class T, class U>
        

T* Create(const U& arg, T)
        

{
        

    return new T(arg);
        

}
        

template <class U>
        

Widget* Create(const U& arg, Widget)
        

{
        

    return new Widget(arg, -1);
        

}
        

// Use Create()
        

String* pStr = Create("Hello", String());
        

Widget* pW = Create(100, Widget());
        

The second parameter of Create serves only for selecting the appropriate overload. It also is the trouble with this would-be idiom: you waste time constructing an arbitrarily complex object that you don't use. Even if the optimizer might help you with that, nothing helps if Widget disables its default constructor.

The proverbial extra level of indirection can help here, too. An idea is to pass T* instead of T as the dummy template parameter. At runtime, you can always pass the null pointer, which is reasonably cheap to construct.

template <class T, class U>
        

T* Create(const U& arg, T*)
        

{
        

    return new T(arg);
        

}
        

template <class U>
        

Widget* Create(const U& arg, Widget*)
        

{
        

    return new Widget(arg, -1);
        

}
        

// Use Create()
        

String* pStr = Create("Hello", (String*)0);
        

Widget* pW = Create(100, (Widget*)0);
        

This way is at best confusing for Create's users. To eternalize this solution as an idiom, we can use a little template similar to Int2Type.

template <typename T>
        

struct Type2Type
        

{
        

    typedef T OriginalType;
        

};
        

Now you can write:

template <class T, class U>
        

T* Create(const U& arg, Type2Type<T>)
        

{
        

    return new T(arg);
        

}
        

template <class U>
        

Widget* Create(const U& arg, Type2Type<Widget>)
        

{
        

    return new Widget(arg, -1);
        

}
        

// Use Create()
        

String* pStr = Create("Hello", Type2Type<String>());
        

Widget* pW = Create(100, Type2Type<Widget>());
        

This certainly is more explanatory than an alternative solution. Again, you can include Type2Type in a library and document its intended use.

Detecting Convertibility and Inheritance

In implementing template functions and classes, a question that arises every so often is: given two arbitrary types B and D, how can you detect whether D inherits from B or not?

Discovering such relationships at compile time is key to implementing advanced optimizations in generic libraries. In a generic function, you can rely on an optimized algorithm if a class implements a certain interface, without having to apply a dynamic_cast for that.

Detecting inheritance relies on a more general mechanism, that of detecting convertibility. The more general problem that we will solve together is how to detect whether an arbitrary type T supports automatic conversion to an arbitrary type U?

There is a solution to this problem, and it relies on sizeof. (You thought the only use for sizeof was as an argument to memset, huh?) There is a surprising amount of power in sizeof; this is because you can apply sizeof to any expression, no matter how complex, and sizeof returns its size, without actually evaluating that expression at runtime. This means that sizeof is aware of overloading, template instantiation, conversion rules — everything that can take part in a C++ expression. In fact, sizeof is a complete facility for deducing the type of an expression; eventually, sizeof throws away the expression and returns you only the size of its result [3].

The idea of conversion detection relies on using sizeof with overloaded functions. You cunningly provide two overloads of a function: one accepts the type to convert to (U), and the other accepts just about anything else. You call the overloaded function with T, whose convertibility to U you want to determine. If the function that accepts a U gets called, you know that T is convertible to U; if the "fallback" function gets called, then T is not convertible to U.

To detect which function gets called, you arrange the two overloads to return types of different sizes, and discriminate with sizeof. The types themselves do not matter, as long as they have different sizes.

Let's first create two types of different sizes. (Apparently, char and long double do have different sizes, but that's not guaranteed by the standard.) A foolproof scheme would be the following:

typedef char Small;
        

struct Big { char dummy[2]; };
        

By definition, sizeof(Small) is one. Also, the size of Big is unknown, but it's for sure greater than one, which is the only guarantee we need.

Next, we need the two overloads. One accepts a U and returns, say, a Small.

Small Test(U);
        

How do you write a function that accepts "anything else"? A template is not a solution because the template would qualify as the best match, thus hiding the conversion. We need a worse match than an automatic conversion. A quick look through the conversion rules applied for a function call gives the ellipsis match, which is the worst of all — the bottom of the list. That's exactly what the doctor prescribed.

Big Test(...);
        

(Calling a function with ellipsis with a C++ object has undefined results, but who cares? Nobody actually calls the function. It's not even implemented.)

Now we need to apply sizeof to the call of Test, passing it a T:

const bool convExists =
        

    sizeof(Test(T())) == sizeof(Small);
        

That's it! The call of Test gets a default-constructed object, T, and then sizeof extracts that expression's result size. It can be either sizeof(Small) or sizeof(Big), depending on whether the compiler found a conversion or not.

There is one little problem. What if T makes its default constructor private? In this case, the expression T fails to compile and so does all of our scaffolding. Fortunately, there is a simple solution — just use a strawman function returning a T. In this case, the compiler is happy and so are we.

T MakeT();
        

const bool convExists =
        

    sizeof(Test(MakeT())) == sizeof(Small);
        

(By the way, isn抰 it nifty just how much you can do with functions, like MakeT and Test, which not only don抰 do anything, but which don抰 even really exist at all?)

Now that we have it working, let's package everything in a class template that hides all the details of type deduction and exposes the result only.

template <class T, class U>
        

class Conversion
        

{
        

    typedef char Small;
        

    struct Big { char dummy[2]; };
        

    static Small Test(U);
        

    static Big Test(...);
        

    T MakeT();
        

public:
        

    enum { exists =
        

        sizeof(Test(MakeT())) == sizeof(Small) };
        

};
        

Now you can test the Conversion template class by writing:

int main()
        

{
        

    using namespace std;
        

    cout
        

        << Conversion<double, int>::exists << ' '
        

        << Conversion<char, char*>::exists << ' '
        

        << Conversion<size_t, vector<int> >::exists << ' ';
        

}
        

This little program prints "1 0 0". Note that, although std::vector does implement a constructor taking a size_t, the conversion test returns zero because that constructor is explicit.

We can implement two more constants inside Conversion:

·   exists2Way, which says whether there are conversions between T and U in both directions. This is the case with int and double, for example, but various user-defined types can implement such two-way conversions.

·   sameType, which is true if T and U are the same type.

template <class T, class U>
        

class Conversion
        

{
        

    ... as above ...
        

    enum { exists2Way = exists && 
        

        Conversion<U, T>::exists };
        

    enum { sameType = false };
        

};
        

We implement sameType through a partial specialization of Conversion.

template <class T>
        

class Conversion<T, T>
        

{
        

public:
        

    enum { exists = 1, exists2Way = 1, sameType = 1 };
        

};
        

Hey, what about that inheritance detection thing? The nicest part is, once you抳e got the conversion thing, detecting inheritance is easy.

#define SUPERSUBCLASS(B, D) /
        

    (Conversion<const D*, const B*>::exists && /
        

    !Conversion<const B*, const void*>::sameType)
        

Obvious? Maybe still blurred a bit. SUPERSUBCLASS(B, D) evaluates to true if D inherits from B publicly, or if B and D represent the same type. SUPERSUBCLASS does its job by evaluating the convertibility from a const D* to a const B*. There are only three cases when const D* converts implicitly to const B*:

·   B is the same type as D;

·   B is an unambiguous public base of D;

·   B is void.

The last case is eliminated by the second test. Accepting the first case (B same as D) is useful in practice, because for practical purposes you can often consider that a class is its own superclass. If you need a stricter test, you can write it like so:

#define SUPERSUBCLASS_STRICT(B, D) /
        

    (SUPERSUBCLASS(B, D) && /
        

    !Conversion<const B, const D>::sameType)
        

Why does the code add all those const modifiers? The reason is that you don't want the conversion test to fail due to const issues. Therefore, the code uses const all over the place; if template code applies const twice (to a type that's already const), the second const is ignored. In a nutshell, by using const in SUPERSUBCLASS, we're always on the safe side.

Why SUPERSUBCLASS and not the cuter BASE_OF or INHERITS? For a very practical reason: with INHERITS(B, D), I kept forgetting which way the test works — does it test whether B inherits D or vice versa? SUPERSUBCLASS(B, D) makes it clearer (at least for me) which one is first and which one is second.

Conclusion

The single most important thing about the three idioms presented here is that they are reusable. You can write them in a library and let programmers use them, as opposed to asking them to grasp their intricate inner workings.

Reusability of nontrivial techniques is important. Ask people to memorize a complicated technique that helps them do something that they can do in simpler, if a bit more tedious, ways; they won't use it. Give people a simple black box that does a little helpful wonder; they might like it and use it because it's in a way "free."

Int2Type, Type2Type, and especially Conversion belong to a generic utilities library. They extend the compile-time abilities of a programmer, by making important compile-time type inferences.

Acknowledgments

If Clint Eastwood asked me "Do you feel lucky?", I'd certainly say I do. This is my third article in a row that benefits from the direct attention, and the great advice, of Herb Sutter himself. Thanks to Tomio Hoshida in Japan for spotting a bug and making insightful suggestions.

Note: This article uses excerpts from Andrei Alexandrescu's upcoming book tentatively titled Modern C++ Design (Addison-Wesley, 2001).

Notes

[1] There is no conceptual hitch to C++ supporting partial specialization of functions, a very desirable (in my opinion) feature.

[2] Andrei Alexandrescu. "Traits: The else-if-then of Types", C++ Report (April 2000).

[3] There is a proposal for adding a typeof operator to C++; that is, an operator returning the type of an expression. Having a typeof would make much template code easier to write and understand. Gnu C++ already implements typeof as an extension. Obviously, typeof and sizeof share the same backend, because sizeof has to compute the type anyway. [Look for Bill Gibbons?article in an upcoming issue of CUJ, where he will present a nifty way to create an (almost) natural typeof operator in today抯 Standard C++. — mb]


About the Author

Andrei Alexandrescu is a Development Manager at RealNetworks Inc. (www.realnetworks.com), based in Seattle, WA, and author of the acclaimed book Modern C++ Design. He may be contacted at andrei@metalanguage.com. Andrei is also one of the featured instructors of The C++ Seminar (www.gotw.ca/cpp_seminar).

 

C++模板详解:泛型编程、模板原理、非类型模板参数、模板特化、分离编译 泛型编程、模板原理、非类型模板参数、模板特化、分离编译 阅读详情

相关推荐

C++:错误:type/value mismatch at argument 1 in template parameter list for ‘template<class ... Types>

文章目录错误代码原因 改正 错误 意思是,在Tuple模板参数列表的第一个参数,发生了类型或者值不匹配问题。 template.cpp: In function ‘int main()’: template.cpp:16:8: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ... Types> struct Tuple’ Tuple<0> error;

mzhan017的博客 4421

STL中的型别

一、常整数映射为型别Alexandrescu提出的简单而有用的templatetemplate struct Int2Type{    enum { value = v };};template class NiftyContainer{private:            void DoSomething(T* pObj, int2Type)  

magicminglee's Blog 662

AI大模型开发——transformer模型超全完整版(0基础可懂)

为了更深入的了解transformer模型,我们首先从transformer的诞生聊起吧。transformer在2017年由Vaswani等人首次提出,它摒弃了之前流行的循环神经网络和卷积神经网络的架构,引入了自注意力机制( Self-Attention),显著提高了处理序列数据的能力和效率。transformer模型的这种设计使其在处理长距离依赖问题时, 相比于传统的循环神经网络和长短期记忆网络(LSTM)表现得更加出色,至此, 开启了NLP技术的新篇章。

m0_74922316的博客 2128

[转]C++ template —— 模板基础(一)

《C++ Template》对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值)。--------------------------------------------------------------------------------------------------...

anjie1937的博客 423

带挂牵引车前向运动模型推导及仿真实现

牵引车-半挂车的车辆结构与普通的车辆相比增加了一个半挂车,它们之间通过鞍座相连接,为了分析半挂牵引车运动过程中的位姿和状态,本文将基于车辆的机械结构建立对应的运动学模型,该模型基于以下假设: 车辆行驶过程中,道路平坦且无凸凹; 车辆低速行驶,且运行过程中车身无侧倾和俯仰运动; 车辆行驶过程中,轮胎无形变,无侧向滑动,且无侧向力; 牵引车和半挂车均满足阿克曼转向几何原理; 牵引车与半挂车不会发现折叠。

jianmo的博客 3207

STM32CubeMX——循迹传感器TCRT5000的使用

前言 人生如逆旅,我亦是行人。 ————苏轼《临江仙·送钱穆父》 一、介绍 TCRT5000描述:(带有晶体管输出的反射光学传感器) TCRT5000是一种反射式光学传感器,它包括一个红外发射器和一个3.5mm引线封装的光电晶体管,可阻挡可见光。还具有1mA 典型被测输出电流,2.5mm峰值工作距离,可应用传感与仪表,工业等行业。 TCRT5000具有紧凑的结构,其中发射光源和探测器布置在同一方向,通过使用来自物体的反射红外光束来感知物体的存在。工作波长为950毫米,探测器由光电晶体管组成

W_Oilpicture的博客 1万+

C++实现编译期快排-模板元编程

https://blog.csdn.net/huanghongxun/article/details/85065406 参考于上面的博客,但具体实现不一样。 #pragma once #include<iostream> namespace mystd { template <typename T, T v> struct integral_constant { ...

weixin_39057744的博客 1812

rk3576 适配摄像头 (mipi 五摄)

在RK3576上实现五路CSI摄像头方案,核心思路是充分复用其视频输入接口,并通过巧妙的软件配置来实现。

weixin_35723192的博客 197

Generic Programming: Mappings between Types and Values (转)

Generic Programming: Mappings between Types and Values (转)[@more@]XML:namespace prefix = o ns = "urn:schemas-micro...

csd3176的博客 246

8、音频运行时系统的架构设计

本文深入探讨了音频运行时系统的分层设计及其优势,包括逻辑层和物理层的功能划分。通过分层设计,开发者可以更轻松地管理和优化音频资源,确保其在不同平台上的高效运行。文章还介绍了平台适配与优化、错误处理与调试以及未来发展方向等内容,为开发者提供了宝贵的参考和指导。

juice的博客 471

物料分类账-返工工单(自身)差异分摊逻辑

如上图所示,当月入库的全部消耗,而返工消耗的数量为100PCS,所以返工消耗占总消耗的比例为100pcs/(205pcs+100pcs)=0.327868852。备注:红色箭头的物料号为产品自己本身所产生的价差,就是返工工单所承担的差异,而我们要计算的就是这个值,所以计算价差不会包括此项。价差合计就是不包括初始分摊的价差9153.7,剩下的价差合计就是返工工单自身所承担的差异4465.21。每一次返工工单所承担的差异都会按照价差*承担比例去计算承担的差异,知道承担完所有差异。

Yeol827的博客 2515

OS23.【Linux】初识进程地址空间

深入探讨了进程地址空间的概念及其在操作系统中的重要作用。首先回顾了C/C++内存区域划分,通过实验验证了栈区向下增长、堆区向上增长的特性。重点阐述了进程地址空间的本质是内核数据结构,包含页表、CR3寄存器等关键组件,以及写时拷贝机制的工作原理。通过分析mm_struct结构体,详细说明了操作系统如何管理进程地址空间。文章还解释了进程地址空间存在的意义:保护物理内存、实现进程隔离,并指出进程地址空间不等于实际内存。最后介绍了操作系统的懒加载机制和缺页中断原理,更新了进程的组成公式为"内核数据

zhangcoder的博客 记录学习轨迹~ 1303

【ComfyUI专栏】ComfyUI 数字人方案-ComfyUI-EchoMimic

EchoMimic是一个基于图片和语音的数字人生成方案,通过ComfyUI节点实现。该方案安装简单,需下载特定模型文件(Unet、AudioProcessor和VAE)并保存本地。相比其他数字人生成方案,其节点操作更简便,支持多种Pose姿态选择,并能结合视频内容进行姿态同步,通过音频同步口型实现动态内容创建。生成的数字人效果直观,适用于快速创建个性化数字形象。

技术与管理的平衡木- 484

type/value mismatch at argument 1

转自:http://www.programlife.net/type-value-mismatch-at-argument-1.html 最近又开始出没于CSDN,看到网友问到STL的提问,而正好自己也在看STL,所以就回答有关STL的问题: 问:关于C++ STL运用的问题。求高手解答下 是关于一个STL运用的问题,有一个模板类,为 template class Word {

5996

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

template技术之数字型别化实现编译期分派工作

数字型别化 template<int v> struct Int2Type { enum { value = v }; }; 作用: 根据引数所得的不同数值来产生不同型别。 eg: Int2Type<1>与Int2Type<2>是不同的数据类型,通过它可以在编译期选择不同数据类型,即编译期的条件判断。 使用场景需满足以下两个条件: 1、有必要根据某个编译期常数调用一个或数个不同的函数。 2、有必要在编译期实施“分派”( dispatch) 。 例子 选择函数 temp

xunye的博客 151

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

C++学习-诡异错误之rank

背景: 修改代码后,出现了很诡异的错误: error: type/value mismatch at argument 1  in template parameter list for‘template struct std::rank’      } else if (y->rank rank) { 解决: 错误中,x和y是两个类对象,r

yngki的博客 4283

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

学习slam14讲时,sophus库的模板/非模板类解决。

学习slam14讲时,sophus库的模板/非模板类解决。 笔者是刚研一准备搞slam的小白,在用sophus::SE3时,报错 error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’ 等等,一大堆错误,然后找到了一些解决方案: 首先关于一个usleep的解决方法, 添加头文件#include <

qq_38720453的博客 1337

argument type mismatch异常解决办法

我们在利用struts框架开发项项目时,会通过表单来提交数据。数据包括直接从文本框接收用户的输入,也有通过导入文件的方式,以及导入本地图片等。     直接从文本框输入的话,可以提交至服务器。在提交文本文件数据时,会出现argument type mismatch异常。详细异常如下: java.lang.IllegalArgumentException: argument type misma...

june696295的专栏 4141

模板显式特化之template<>语法

最近在拜读侯捷老师的《STL源码剖析》,本着勤于实践的指导思想,使用gcc和vc分别编译运行了第27页的程序,此程序是用来测试SGI STL中的__STL_STATIC_TEMPLATE_MEMBER_BUG宏的。其代码如下: #include using namespace std; template class testclass { public:     stat

WDSM的专栏 6800

template详解

1. 函数模板1. 函数模板定义关键字:“template开头”,“参数列表非空” 定义一定要以:template<…>开头(为了解释为什么 inline 在第 2 位置) 定义中模板参数列表不能为空(模板特例化中参数列表可以为空)。template<tempname T> inline T funcName(const T &a, const T &b) { return (a + b);

TonyaBaSy 5273

【C++泛型编程】模板偏特化、局部类和型别映射(Int2Type,Type2Type)以及型别选择

1.模板偏特化          模板偏特化是让你在template的所有可能实体中特化出一组子集。 下面是一个模板全特化的例子,假设有一个类模板,名为Widget: template class Widget { ....各种操作..... }; 特化的情况如下: template class Widget { ...各种特化后的操作... }; 其中ModalDia

xiaoding133的专栏【Stay hungry,Stay foolish】 2941

argument type mismatch异常

在使用实体类的set()方法向mysql存入数据的时候,报了异常: 这个时候完全找不到错误的来源,但是不要慌,利用起try...catch()语句 : try {             boolean rlt = studyuserService.insertOrUpdate(users);         }catch (Exception e) {             Sy...

TimeJjc的博客 6622

SSD预测错误调试:RuntimeError: index_select(): functions with out=... arguments don‘t support automatic dif

SSD测试时报错

XiaoGShou的博客 3097
上一篇: C++模板的定制五:对定制成员函数的补充
下一篇: Obfuscated C++
winmain
博客等级 码龄26年 5粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址