We can solve any problem by introducing an extra level of indirection

As reading the C++ template metaprogramming, I learn the famous saying in software engineering again. How to understand this remark in the context of C++ template metaprogramming?

What we have to mention is the type trait skill, I‘d like to reference the demo in the book:

template <class ForwardIterator1, class ForwardIterator2>
    void iter_swap(ForwardIterator1 i1, ForwardIterator2 i2)
    {
        typename                      // (see Language Note)
          ForwardIterator1::value_type tmp = *i1;
        *i1 = *i2;
        *i2 = tmp;
    }
We can make this function work only if the template parameter ForwardIterator1 has a "Property" named as value type; What if we assign a plain int* to ForwardIterator1?The compiler will complain there is no value_type defined in int*. But int* is exactly an iterable type, we have to fix this problem. The mean we introduce is to setup an extra level of indirection. The extra level of indirection is the trait:
    template <class Iterator>
    struct iterator_traits {
        typedef typename Iterator::value_type value_type;
        ...four more typedefs
    };

Then we can use this level of indirection to be a adapter for the typing difference;

    template <class ForwardIterator1, class ForwardIterator2>
    void iter_swap(ForwardIterator1 i1, ForwardIterator2 i2)
    {
        typename
          iterator_traits<ForwardIterator1>::value_type tmp = *i1;
        *i1 = *i2;
        *i2 = tmp;
    }

In case of int*, we use partial specialization syntax of C++

    template <>
    struct iterator_traits<T*>
    {
        typedef T value_type;
        four more typedefs...
    };

The compiler will not complain because of the existance of the extra level of indirection: traits

Traits achieve the type recognization non-intrusively, "the generic function can access the type uniformly";

 
 
时间: 2024-10-24 19:03:34

We can solve any problem by introducing an extra level of indirection的相关文章

solve the problem of &#39;java web project cannot display verification code&#39;

my java code of the function: package com.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.servlet.ServletException;

ubuntu:solve the problem of &#39;E:Problem with MergeList /var/lib/apt/lists/&#39;

just run this command: sudo rm /var/lib/apt/lists/* -vfR it will remove all the software package with the state of 'apt-get install' and no use to leave them, that's ok to just r.m. other conditons can refer to this article:http://blog.csdn.net/gopai

How to solve the problem : &quot;You have been logged on with a temporary profile&quot;

/*By Jiangong SUN*/ I've encountered a problem in one server, which is : Every time I login into the server, it creates a new temporary user profile for my account. Here is the error message: "You have been logged on with a temporary profile. You can

2.7 编程之美--最大公约数的3种解法[efficient method to solve gcd problem]

[本文链接] http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html [题目] 求两个正整数的最大公约数Greatest Common Divisor (GCD).如果两个正整数都很大,有什么简单的算法吗?例如,给定两个数1 100 100 210 001, 120 200 021,求出其最大公约数. [解法] [1. 辗转相除法] 辗转相除法:f(x,y) = f(y , x % y)(x>y

How to solve the problem that Github can&#39;t visit in China?

find path C:\Windows\System32\drivers\etc\host open DNS detection and DNS query-Webmaster(DNS查询) tool (http://tool.chinaz.com/dns) enter the domain name in the test input field (example: github.com) input the IP with the lowest TTL value in the detec

C++ Core Guidelines

C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very early draft. It is inkorrekt, incompleat, and pµøoorly formatted. Had it been an open source (code) project, this would have been release 0.6. Copy

DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS

FIELD OF INVENTION This invention relates to computer graphics processing, and more specifically to computer graphics processing using two or more architecturally distinct graphics processors. BACKGROUND OF INVENTION Many computing devices utilize hi

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

windows消息机制详解(转载)

消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的,这个记录中包含了消息的类型以及其他信息.例如,对于单击鼠标所产生的消息来说,这个记录中包含了单击鼠标时的坐标.这个记录类型叫做TMsg, 它在Windows单元中是这样声明的:typeTMsg = packed recordhwnd: HWND; / /窗口句柄message: UINT; / /