随笔-10  评论-1  文章-0  trackbacks-0
  2011年9月5日
   今天在编译模拟器的时候,移植一段代码,发现怎么都编译不过,总是报c:\program files\microsoft visual studio\vc98\include\windef.h(141) : error C2632: 'long' followed by 'long' is illegal 错误。
   发现是 头文件可能最后少},; #endif 之类的东西。
posted @ 2011-09-05 18:40 Mr.Johnson 阅读(3660) | 评论 (0)编辑 收藏
  2011年4月1日
    今天看《STL templates:The Complete Guide》看到有关于Restrictions for Nontype Template Parameters 部分,提出了一个有关于external linkage概念,网上一搜基本上都是同一篇中文的有关于internal linkage 和 external linkage的解释,看了半天我还是有点迷糊,用google搜到一本书叫做The C Book 的书里面有有关于这个概念的介绍。
    在C语言中有两种linkage object:
        1.external object
            external object就是函数外的声明。
        2.internal object
            internal object就是函数内的声明。

    在C语言中有三种linkage:
        1.external linkage
        2.internal linkage
        3.none linkage
    有关于external linkage 就是所谓的全局变量作用域,internal linkage就是只能在单个文件内使用的变量的作用域,而none linkage 就是那些函数的参数作用域。
Type of linkage Type of object Accessibility
external external throughout the program
internal external a single file
none internal local to a single function

posted @ 2011-04-01 17:58 Mr.Johnson 阅读(838) | 评论 (0)编辑 收藏
  2011年2月11日
时间日期函数,函数库为time.h
在时间日期函数里,主要用到的结构有以下几个:
总时间日期贮存结构tm
┌──────────────────────┐
│struct tm │
│{ │
│ int tm_sec; /*秒,0-59*/ │
│ int tm_min; /*分,0-59*/ │
│ int tm_hour; /*时,0-23*/ │
│ int tm_mday; /*天数,1-31*/ │
│ int tm_mon; /*月数,0-11*/ │
│ int tm_year; /*自1900的年数*/ │
│ int tm_wday; /*自星期日的天数0-6*/ │
│ int tm_yday; /*自1月1日起的天数,0-365*/ │
│ int tm_isdst; /*是否采用夏时制,采用为正数*/│
│} │
└──────────────────────┘
日期贮存结构date
┌───────────────┐
│struct date │
│{ │
│ int da_year; /*自1900的年数*/│
│ char da_day; /*天数*/ │
│ char da_mon; /*月数 1=Jan*/ │
│} │
└───────────────┘
时间贮存结构time
┌────────────────┐
│struct time │
│{ │
│ unsigned char ti_min; /*分钟*/│
│ unsigned char ti_hour; /*小时*/│
│ unsigned char ti_hund; │
│ unsigned char ti_sec; /*秒*/ │
│} │
└────────────────┘
char *ctime(long *clock)
  本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
  字符串:Mon Nov 21 11:31:54 1983\n\0
char *asctime(struct tm *tm)
  本函数把指定的tm结构类的时间转换成下列格式的字符串:
  Mon Nov 21 11:31:54 1983\n\0
double difftime(time_t time2,time_t time1)
  计算结构time2和time1之间的时间差距(以秒为单位)
struct tm *gmtime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)
  转换成格林威治时间,并以tm结构形式返回
struct tm *localtime(long *clock)本函数把clock所指的时间(如函数time返回的时间)
  转换成当地标准时间,并以tm结构形式返回
void tzset()本函数提供了对UNIX操作系统的兼容性
long dostounix(struct date *dateptr,struct time *timeptr)
  本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回
  自格林威治时间1970年1月1日凌晨起到现在的秒数
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
  本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成
  DOS格式并保存于用户所指的结构dateptr和timeptr中
void getdate(struct date *dateblk)本函数将计算机内的日期写入结构dateblk
  中以供用户使用
void setdate(struct date *dateblk)本函数将计算机内的日期改成
  由结构dateblk所指定的日期
void gettime(struct time *timep)本函数将计算机内的时间写入结构timep中,
  以供用户使用
void settime(struct time *timep)本函数将计算机内的时间改为
  由结构timep所指的时间
long time(long *tloc)本函数给出自格林威治时间1970年1月1日凌晨至现在所经
  过的秒数,并将该值存于tloc所指的单元中.
int stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)
  写入计算机中.
posted @ 2011-02-11 10:00 Mr.Johnson 阅读(398) | 评论 (0)编辑 收藏
  2010年12月12日

C++ Templates Tutorial

C++ Library  
 

Table of Contents

Introduction

Class Templates

Function Templates

Template Instantiation

Class Template Specialization

Template Function Specialization

Template Parameters

Static Members and Variables

Templates and Friends


Introduction

Many C++ programs use common data structures like stacks, queues and lists. A program may require a queue of customers and a queue of messages. One could easily implement a queue of customers, then take the existing code and implement a queue of messages. The program grows, and now there is a need for a queue of orders. So just take the queue of messages and convert that to a queue of orders (Copy, paste, find, replace????). Need to make some changes to the queue implementation? Not a very easy task, since the code has been duplicated in many places. Re-inventing source code is not an intelligent approach in an object oriented environment which encourages re-usability. It seems to make more sense to implement a queue that can contain any arbitrary type rather than duplicating code. How does one do that? The answer is to use type parameterization, more commonly referred to as templates.

C++ templates allow one to implement a generic Queue<T> template that has a type parameter T. T can be replaced with actual types, for example, Queue<Customers>, and C++ will generate the class Queue<Customers>. Changing the implementation of the Queue becomes relatively simple. Once the changes are implemented in the template Queue<T>, they are immediately reflected in the classes Queue<Customers>, Queue<Messages>, and Queue<Orders>.

Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code.

C++ provides two kinds of templates: class templates and function templates. Use function templates to write generic functions that can be used with arbitrary types. For example, one can write searching and sorting routines which can be used with any arbitrary type. The Standard Template Library generic algorithms have been implemented as function templates, and the containers have been implemented as class templates.

Class Templates

Implementing a class template

A class template definition looks like a regular class definition, except it is prefixed by the keyword template. For example, here is the definition of a class template for a Stack.

template <class T>
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&); 
	int pop(T&) ;  
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // number of elements on Stack.
	int top ;  
	T* stackPtr ;  
} ;

T is a type parameter and it can be any type. For example, Stack<Token>, where Token is a user defined class. T does not have to be a class type as implied by the keyword class. For example, Stack<int> and Stack<Message*> are valid instantiations, even though int and Message* are not "classes".

Implementing class template member functions

Implementing template member functions is somewhat different compared to the regular class member functions. The declarations and definitions of the class template member functions should all be in the same header file. The declarations and definitions need to be in the same header file. Consider the following.

//B.H
template <class t>
class b
{
public:
	b() ;
	~b() ;
} ;
// B.CPP
#include "B.H"
template <class t>
b<t>::b()
{
}
template <class t>
b<t>::~b()
{
}
//MAIN.CPP
#include "B.H"
void main()
{
	 b<int> bi ;
	 b <float> bf ;
}

When compiling B.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there are two instantiations: template class B<int> and B<float>. At this point the compiler has the declarations but no definitions!

While implementing class template member functions, the definitions are prefixed by the keyword template. Here is the complete implementation of class template Stack:

//stack.h
#pragma once
template <class T>
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&); 
	int pop(T&) ;  // pop an element off the stack
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // Number of elements on Stack
	int top ;  
	T* stackPtr ;  
} ;

//constructor with the default size 10
template <class T>
Stack<T>::Stack(int s)
{
	size = s > 0 && s < 1000 ? s : 10 ;  
	top = -1 ;  // initialize stack
	stackPtr = new T[size] ; 
}
 // push an element onto the Stack 
template <class T>
int Stack<T>::push(const T& item)
{
	if (!isFull())
	{
		stackPtr[++top] = item ;
		return 1 ;  // push successful
	}
	return 0 ;  // push unsuccessful
}

// pop an element off the Stack
template <class T> 
int Stack<T>::pop(T& popValue) 
{
	if (!isEmpty())
	{
		popValue = stackPtr[top--] ;
		return 1 ;  // pop successful
	}
	return 0 ;  // pop unsuccessful
}

Using a class template

Using a class template is easy. Create the required classes by plugging in the actual type for the type parameters. This process is commonly known as "Instantiating a class". Here is a sample driver class that uses the Stack class template.

#include <iostream>
#include "stack.h"
using namespace std ;
void main()
{
	typedef Stack<float> FloatStack ;
	typedef Stack<int> IntStack ;

	FloatStack fs(5) ;
	float f = 1.1 ;
	cout << "Pushing elements onto fs" << endl ;
	while (fs.push(f))
	{
		cout << f << ' ' ;
		f += 1.1 ;
	}
	cout << endl << "Stack Full." << endl
	<< endl << "Popping elements from fs" << endl ;
	while (fs.pop(f))
		cout << f << ' ' ;
	cout << endl << "Stack Empty" << endl ;
	cout << endl ;

	IntStack is ;
	int i = 1.1 ;
	cout << "Pushing elements onto is" << endl ;
	while (is.push(i))
	{
		cout << i << ' ' ;
		i += 1 ;
	}
	cout << endl << "Stack Full" << endl
	<< endl << "Popping elements from is" << endl ;
	while (is.pop(i))
			cout << i << ' ' ;
	cout << endl << "Stack Empty" << endl ;
}

Program Output

Pushing elements onto fs
1.1 2.2 3.3 4.4 5.5 
Stack Full.

Popping elements from fs
5.5 4.4 3.3 2.2 1.1 
Stack Empty

Pushing elements onto is
1 2 3 4 5 6 7 8 9 10 
Stack Full

Popping elements from is
10 9 8 7 6 5 4 3 2 1 
Stack Empty

In the above example we defined a class template Stack. In the driver program we instantiated a Stack of float (FloatStack) and a Stack of int(IntStack). Once the template classes are instantiated you can instantiate objects of that type (for example, fs and is.)

A good programming practice is using typedef while instantiating template classes. Then throughout the program, one can use the typedef name. There are two advantages:

  • typedef's are very useful when "templates of templates" come into usage. For example, when instantiating an STL vector of int's, you could use:
    		 typedef vector<int, allocator<int> > INTVECTOR ; 
    	
  • If the template definition changes, simply change the typedef definition. For example, currently the definition of template class vector requires a second parameter.
    		typedef vector<int, allocator<int> > INTVECTOR ;
    		INTVECTOR vi1 ;
    	
    In a future version, the second parameter may not be required, for example,
    		typedef vector<int> INTVECTOR ;
    		INTVECTOR vi1 ;
    	

Imagine how many changes would be required if there was no typedef!

Function Templates

To perform identical operations for each type of data compactly and conveniently, use function templates. You can write a single function template definition. Based on the argument types provided in calls to the function, the compiler automatically instantiates separate object code functions to handle each type of call appropriately. The STL algorithms are implemented as function templates.

Implementing Template Functions

Function templates are implemented like regular functions, except they are prefixed with the keyword template. Here is a sample with a function template.

#include <iostream>
using namespace std ;
//max returns the maximum of the two elements
template <class T>
T max(T a, T b)
{
	return a > b ? a : b ;
}

Using Template Functions

Using function templates is very easy: just use them like regular functions. When the compiler sees an instantiation of the function template, for example: the call max(10, 15) in function main, the compiler generates a function max(int, int). Similarly the compiler generates definitions for max(char, char) and max(float, float) in this case.

#include <iostream>
using namespace std ;
//max returns the maximum of the two elements
template <class T>
T max(T a, T b)
{
	return a > b ? a : b ;
}
void main()
{
   
	cout << "max(10, 15) = " << max(10, 15) << endl ;
	cout << "max('k', 's') = " << max('k', 's') << endl ;
	cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl ;
}

Program Output

max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2

Template Instantiation

When the compiler generates a class, function or static data members from a template, it is referred to as template instantiation.

  • A class generated from a class template is called a generated class.
  • A function generated from a function template is called a generated function.
  • A static data member generated from a static data member template is called a generated static data member.

The compiler generates a class, function or static data members from a template when it sees an implicit instantiation or an explicit instantiation of the template.

  1. Consider the following sample. This is an example of implicit instantiation of a class template.
    template <class T>
    class Z
    {
      public:
    Z() {} ;
    ~Z() {} ;
    void f(){} ;
    void g(){} ;
    } ;
    
    int main()
    {
    Z<int> zi ;   //implicit instantiation generates class Z<int>
    Z<float> zf ; //implicit instantiation generates class Z<float>
    return 0 ;
    }
    
  2. Consider the following sample. This sample uses the template class members Z<T>::f() and Z<T>::g().
    template <class T>
    class Z
    {
      public:
    Z() {} ;
    ~Z() {} ;
    void f(){} ;
    void g(){} ;
    } ;
    
    int main()
    {
    Z<int> zi ; //implicit instantiation generates class Z<int>
    zi.f() ;    //and generates function Z<int>::f()
    Z<float> zf ; //implicit instantiation generates class Z<float>
    zf.g() ;      //and generates function Z<float>::g()
    return 0 ;
    }
    

    This time in addition to the generating classes Z<int> and Z<float>, with constructors and destructors, the compiler also generates definitions for Z<int>::f() and Z<float>::g(). The compiler does not generate definitions for functions, nonvirtual member functions, class or member class that does not require instantiation. In this example, the compiler did not generate any definitions for Z<int>::g() and Z<float>::f(), since they were not required.

     

  3. Consider the following sample. This is an example of explicit instantiation of a class template.
    template <class T>
    class Z
    {
      public:
    Z() {} ;
    ~Z() {} ;
    void f(){} ;
    void g(){} ;
    } ;
    
    int main()
    {
    template class Z<int> ; //explicit instantiation of class Z<int>
    template class Z<float> ; //explicit instantiation of 
                              //class Z<float>
    return 0 ;
    }
    
  4. Consider the following sample. Will the compiler generate any classes in this case? The answer is NO.
    template <class T>
    class Z
    {
      public:
    Z() {} ;
    ~Z() {} ;
    void f(){} ;
    void g(){} ;
    } ;
    
    int main()
    {
    Z<int>* p_zi ; //instantiation of class Z<int> not required
    Z<float>* p_zf ; //instantiation of class Z<float> not required
    return 0 ;
    }
    

    This time the compiler does not generate any definitions! There is no need for any definitions. It is similar to declaring a pointer to an undefined class or struct.

     

  5. Consider the following sample. This is an example of implicit instantiation of a function template.
    //max returns the maximum of the two elements
    template <class T>
    T max(T a, T b)
    {
        return a > b ? a : b ;
    }
    void main()
    {
    int I ;
    I = max(10, 15) ; //implicit instantiation of max(int, int)
    char c ;
    c = max('k', 's') ; //implicit instantiation of max(char, char)
    }
    

    In this case the compiler generates functions max(int, int) and max(char, char). The compiler generates definitions using the template function max.

     

  6. Consider the following sample. This is an example of explicit instantiation of a function template.
    template <class T>
    void Test(T r_t)
    {
    }
    
    int main()
    {
    //explicit instantiation of Test(int)
    	template void Test<int>(int) ;
    	return 0 ;
    }
    
    NOTE: Visual C++ 5.0 does not support this syntax currently. The above sample causes compiler error C1001.

    In this case the compiler would generate function Test(int). The compiler generates the definition using the template function Test.

     

  7. If an instantiation of a class template is required, and the template declared but not defined, the program is ill-formed. VC5.0 compiler generates error C2079.
    template <class T> class X ;
    
    int main()
    {
    	X<int> xi ; //error C2079: 'xi' uses undefined class 'X<int>'
    	return 0 ;
    }
    
  8. Instantiating virtual member functions of a class template that does not require instantiation is implementation defined. For example, in the following sample, virtual function X<T>::Test() is not required, VC5.0 generates a definition for X<T>::Test.
    template <class T> 
    class X 
    {
    	public:
    		virtual void Test() {} 
    };
    
    int main()
    {
    	X<int> xi ; //implicit instantiation of X<int>
    	return 0 ;
    }
    

    In this case the compiler generates a definition for X<int>::Test, even if it is not required.

Class Template Specialization

In some cases it is possible to override the template-generated code by providing special definitions for specific types. This is called template specialization. The following example defines a template class specialization for template class stream.

#include <iostream>
using namespace std ;

template <class T>
class stream
{
	public:
		void f() { cout << "stream<T>::f()"<< endl ;}
} ;

template <>
class stream<char>
{
	public:
		void f() { cout << "stream<char>::f()"<< endl ;}
} ;

int main()
{
	stream<int> si ;
	stream<char> sc ;

	si.f() ;
	sc.f() ;
	
	return 0 ;
}

Program Output

stream<T>::f()
stream<char>::f()

In the above example, stream<char> is used as the definition of streams of chars; other streams will be handled by the template class generated from the class template.

Template Class Partial Specialization

You may want to generate a specialization of the class for just one parameter, for example

//base template class
template<typename T1, typename T2> 
class X 
{
} ;

//partial specialization
template<typename T1> 
class X<T1, int> 
{
} ; //C2989 here

int main()
{
	// generates an instantiation from the base template
	X<char, char> xcc ; 
	
	//generates an instantiation from the partial specialization
X<char, int> xii ;  

	return 0 ;
}

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list.

NOTE: Visual C++ 5.0 does not support template class partial specialization. The above sample causes compiler error C2989: template class has already been defined as a non-template class.

Template Function Specialization

In some cases it is possible to override the template-generated code by providing special definitions for specific types. This is called template specialization. The following example demonstrates a situation where overriding the template generated code would be necessary:

#include <iostream>
using namespace std ;

//max returns the maximum of the two elements of type T, where T is a
//class or data type for which operator> is defined.
template <class T>
T max(T a, T b)
{
    return a > b ? a : b ;
}

int main()
{    
    cout << "max(10, 15) = " << max(10, 15) << endl ;
    cout << "max('k', 's') = " << max('k', 's') << endl ;
    cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl ;
    cout << "max(\"Aladdin\", \"Jasmine\") = " << max("Aladdin", "Jasmine") << endl ;
    return 0 ;
}

Program Output

max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Aladdin

Not quite the expected results! Why did that happen? The function call max("Aladdin", "Jasmine") causes the compiler to generate code for max(char*, char*), which compares the addresses of the strings! To correct special cases like these or to provide more efficient implementations for certain types, one can use template specializations. The above example can be rewritten with specialization as follows:

#include <iostream>
#include <cstring>
using namespace std ;

//max returns the maximum of the two elements
template <class T>
T max(T a, T b)
{
    return a > b ? a : b ;
}

// Specialization of max for char*
template <>
char* max(char* a, char* b)
{
    return strcmp(a, b) > 0 ? a : b ;
}

int main()
{
    
    cout << "max(10, 15) = " << max(10, 15) << endl ;
    cout << "max('k', 's') = " << max('k', 's') << endl ;
    cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl ;
    cout << "max(\"Aladdin\", \"Jasmine\") = " << max("Aladdin", "Jasmine") << endl ;
    return 0 ;
}

Program Output

max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Jasmine

Template Parameters

  1. C++ templates allow one to implement a generic Queue<T> template that has a type parameter T. T can be replaced with actual types, for example, Queue<Customers>, and C++ will generate the class Queue<Customers>. For example,
    template <class T>
    class Stack
    {
    } ;
    

    Here T is a template parameter, also referred to as type-parameter.

     

  2. C++ allows you to specify a default template parameter, so the definition could now look like:
    	template <class T = float, int elements = 100> Stack { ....} ;
    

    Then a declaration such as

    	Stack<> mostRecentSalesFigures ;	
    

    would instantiate (at compile time) a 100 element Stack template class named mostRecentSalesFigures of float values; this template class would be of type Stack<float, 100>.

    Note, C++ also allows non-type template parameters. In this case, template class Stack has an int as a non-type parameter.

    If you specify a default template parameter for any formal parameter, the rules are the same as for functions and default parameters. Once a default parameter is declared all subsequent parameters must have defaults.

     

  3. Default arguments cannot be specified in a declaration or a definition of a specialization. For example,
    template <class T, int size>
    class Stack
    {
    } ;
    
    //error C2989: 'Stack<int,10>' : template class has already been
    //defined as a non-template class
    template <class T, int size = 10>
    class Stack<int, 10>
    {
    } ;
    
    int main()
    {
    	Stack<float,10> si ;
                  return 0 ;
    }
    

     

  4. A type-parameter defines its identifier to be a type-name in the scope of the template declaration, and canot be re-declared within its scope (including nested scopes). For example,
    template <class T, int size>
    class Stack
    {
    	int T ; //error type-parameter re-defined.
    	void f()
    	{
    		char T ; //error type-parameter re-defined.
    	}
    } ;
    
    class A {} ;
    int main()
    {
    	Stack<A,10> si ;
           return 0 ;
    }
    

    NOTE: VC++ 5.0 or SP1 compiles this sample without any errors. It does not flag the re-definition of type-parameter as an error.

     

  5. The value of a non-type-parameter cannot be assigned to or have its value changed. For example,
    template <class T, int size>
    class Stack
    {
    	void f()
    	{
    		//error C2105: '++' needs l-value             
                 size++ ; //error change of template argument value
    	}
    } ;
    
    
    int main()
    {
    	Stack<double,10> si ;
           return 0 ;
    }
    
  6. A template-parameter that could be interpreted as either a parameter-declaration or a type-parameter, is taken as a type-parameter. For example,
    class T {} ;
    int i ;
    
    template <class T, T i>
    void f(T t)
    {
    	T t1 = i ; //template arguments T and i
    	::T t2 = ::i ; //globals T and i 
    } 
    
    
    
    int main()
    {
    	f('s') ; //C2783 here
    	return 0 ;
    }
    

    NOTE: Compiling the above sample using VC++ 5.0 and SP1 causes compiler error C2783: could not deduce template argument for 'i'. To workaround the problem, replace the call to f('s') with f<char, 's'>('s').

    class T {} ;
    int i ;
    
    template <class T, T i>
    void f(T t)
    {
    	T t1 = i ; //template arguments T and i
    	::T t2 = ::i ; //globals T and i 
    } 
    
    int main()
    {
    	f<char, 's'>('s') ; //workaround
    	return 0 ;
    }
    
  7. A non-type template parameter cannot be of floating type. For example,
    template <double d> class X ; //error C2079: 'xd' uses 
                                  //undefined class 'X<1.e66>'
    //template <double* pd> class X ; //ok
    //template <double& rd> class X ; //ok
    
    int main()
    {
    	X<1.0> xd ;
    	return 0 ;
    }
    

Static Members and Variables

  1. Each template class or function generated from a template has its own copies of any static variables or members.

     

  2. Each instantiation of a function template has it's own copy of any static variables defined within the scope of the function. For example,
    template <class T>
    class X
    {
        public:
    	static T s ;
    } ;
    
    int main()
    {
    	X<int> xi ;
           X<char*> xc ;
    }
    

    Here X<int> has a static data member s of type int and X<char*> has a static data member s of type char*.

     

  3. Static members are defined as follows.
    #include <iostream>
    using namespace std ;
    
    template <class T>
    class X
    {
        public:
    	static T s ;
    } ;
    
    template <class T> T X<T>::s = 0 ;
    template <> int X<int>::s = 3 ;
    template <> char* X<char*>::s = "Hello" ;
    
    int main()
    {
    	X<int> xi ;
    	cout << "xi.s = " << xi.s << endl ;
    
           X<char*> xc ;
    	cout << "xc.s = " << xc.s << endl ;
    	
    	return 0 ;
    }
    

    Program Output

    xi.s = 10
    xc.s = Hello
    
  4. Each instantiation of a function template has it's own copy of the static variable. For example,
    #include <iostream>
    using namespace std ;
    
    template <class T>
    void f(T t)
    {
    	static T s  = 0;
    	s = t ;
    	cout << "s = " << s << endl ;
    } 
    
    int main()
    {
    	f(10) ;
    	f("Hello") ;
    	
    	return 0 ;
    }
    

    Program Output

    s = 10
    s = Hello
    

    Here f<int>(int) has a static variable s of type int, and f<char*>(char*) has a static variable s of type char*.

Templates and Friends

Friendship can be established between a class template and a global function, a member function of another class (possibly a template class), or even an entire class (possible template class). The table below lists the results of declaring different kinds of friends of a class.

Class Template friend declaration in class template X Results of giving friendship
template class <T> class X friend void f1() ; makes f1() a friend of all instantiations of template X. For example, f1() is a friend of X<int>, X<A>, and X<Y>.
template class <T> class X friend void f2(X<T>&) ; For a particular type T for example, float, makes f2(X<float>&) a friend of class X<float> only. f2(x<float>&) cannot be a friend of class X<A>.
template class <T> class X friend A::f4() ; // A is a user defined class with a member function f4() ; makes A::f4() a friend of all instantiations of template X. For example, A::f4() is a friend of X<int>, X<A>, and X<Y>.
template class <T> class X friend C<T>::f5(X<T>&) ; // C is a class template with a member function f5 For a particular type T for example, float, makes C<float>::f5(X<float>&) a friend of class X<float> only. C<float>::f5(x<float>&) cannot be a friend of class X<A>.
template class <T> class X friend class Y ; makes every member function of class Y a friend of every template class produced from the class template X.
template class <T> class X friend class Z<T> ; when a template class is instantiated with a particular type T, such as a float, all members of class Z<float> become friends of template class X<float>.


© 1997 Microsoft Corporation. All rights reserved. Terms of Use.
posted @ 2010-12-12 00:10 Mr.Johnson 阅读(1128) | 评论 (0)编辑 收藏
  2010年11月27日
      最近项目里面经常使用到memcpy,使用memcpy的时候,如下代码
#include<stdio.h>
#include
<string.h>
int main()
{
char * src ="Hello World";
char * dst ="Hello World Again!";
memcpy(dst,src,
sizeof(src));
return 1;
}
报错说内存地址错误..也就是Unhandled exception in .exe:0xC0000005:Access Violation。

     没事就研究了下memcpy ,memcpy在VC6.0版本中的代码如下
 1void * __cdecl memcpy (
 2        void * dst,
 3        const void * src,
 4        size_t count
 5        )
 6{
 7        void * ret = dst;
 8
 9#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
10        {
11        extern void RtlMoveMemory( void *const void *, size_t count );
12
13        RtlMoveMemory( dst, src, count );
14        }

15#else  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
16        /*
17         * copy from lower addresses to higher addresses
18         */

19        while (count--{
20                *(char *)dst = *(char *)src;
21                dst = (char *)dst + 1;
22                src = (char *)src + 1;
23        }

24#endif  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
25
26        return(ret);
27}

28
实际上 memcpy 使用的代码是从第19行开始的
void * __cdecl memcpy (
        
void * dst,
        
const void * src,
        size_t count
        )
{
        
void * ret = dst;
        
while (count--{
                
*(char *)dst = *(char *)src;
                dst 
= (char *)dst + 1;
                src 
= (char *)src + 1;
        }

        
return(ret);
}

为了可以进入 中进行调试
我使用以下代码调试到memcpy中 
#include<stdio.h>
void * __cdecl memcpy (
        
void * dst,
       
const void * src,
       size_t count
        )
{
        
void * ret = dst;

       
while (count--{
               
*(char *)dst = *(char *)src;
                dst 
= (char *)dst + 1;
                src 
= (char *)src + 1;
       }

        
return(ret);
}




int main()
{
char * src ="Hello World";
char * dst ="Hello World Again!";
memcpy(dst,src,
sizeof(src));
return 1;
}
最后发现错误出在memcpy函数中
*(char *)dst = *(char *)src;
为什么呢?
char * dst ="Hello World Again!";
这里dst是在文字常量区分配一个字符串常量,再把str1指向它
等于就是
const char * dst ="Hello World Again!";
所以这里已给字符指针所指向的字符变量赋值就出错。
如果要想使以上代码顺利运行 可以将main中的代码改为如下代码

int main()
{
char * src ="Hello World";
char * dst =new char[20];
memcpy(dst,src,
sizeof(src));
return 1;
}
或者

int main()
{
char * src ="Hello World";
char dst[20];
memcpy(dst,src,
sizeof(src));
return 1;
}
posted @ 2010-11-27 23:44 Mr.Johnson 阅读(1907) | 评论 (1)编辑 收藏
  2010年11月21日
  加入一个CDateTimeCtrl 控件 进对话框后居然报错CDateTimeCtrl' : missing storage-class or type specifiers
http://www.codeguru.com/forum/showthread.php?t=50341
如下处理
  #7    
Old May 28th, 1999, 07:03 PM
Member
 
Join Date: Apr 1999
Location: Philippines
Posts: 46
Tonio is an unknown quantity at this point (<10)
Re: CDateTimeCtrl problem

Good Day!
including the "AFXDTCTL.H" in my stdafx.h file made my day!
many thanks to ric,walter i an and ERIC33 for the support!
posted @ 2010-11-21 20:51 Mr.Johnson 阅读(845) | 评论 (0)编辑 收藏
  2010年5月26日

今天没事用VS2005打开一个COM应用原理的源代码,一运行就报错,然后修改来修改去,最终有2个主要错误:
1. 不能将参数  从“const wchar_t [ ]”转换为“String”

2.无法解析的外部符号 "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)“

第一个问题我发现源码中String被定义为
typedef unsigned short *String;
翻阅MSDN里面有段话

By providing overloads for both the unsigned short and __wchar_t variations of wchar_t, you can create libraries that can easily be linked with code compiled with or without /Zc:wchar_t and avoid the need to provide two different builds of the library (one with and one without /Zc:wchar_t enabled).

意思是要不使用内置的wchar_t类型来避免这两个不同的库冲突
第二个问题也是一样的
具体原因是你导入的库对于wchar_t不是内置类型,以前的vc6都不是,而vc6.0之后的工程缺省都是wchar_t是内建类型,其导出出来的符号是不一样的。所以,你只需要在vc7的project/proterties里面的c/c++   -->language     -->treat   wchar_t   as   build_in   type设置为no,就可以和以前那些库兼容了。所有使用了wchat_t的函数都会有这个问题。
posted @ 2010-05-26 05:24 Mr.Johnson 阅读(461) | 评论 (0)编辑 收藏
  2010年1月25日
      在使用JRTPLIB的发送数据的时候需要设置时间戳单位(timestamp)和时间戳增量(timestamp increment)。看了网上一些文章,细细想来现在才想通这个问题。   
         RFC3550对时间戳的描述是:
 

        时间戳(timestamp) 32比特 时间戳反映了RTP数据包中第一个字节的采样时间。(采样时钟必须来源于一个及时的单调、线性递增时钟,以便允许同步和去除网络引起的数据包抖动(见章节6.4.1)。该时钟的分辨率必须满足理想的同步精度和测量数据包到来时的抖动的需要(一种典型的时钟分辨率不满足情况是每个视频帧仅一个时钟周期)时钟频率依赖于负载数据的格式,并在描述文件(profile)中或者是在负载格式描述中(payload format speci_cation)进行静态描述。也可以通过非RTP方法(non-RTP means)对负载格式动态描述。

       如果RTP包是周期性产生的,那么将使用由采样时钟决定的名义上的采样时刻,而不是读取系统时间。例如,对一个固定速率的音频,采样时钟(时间戳时钟)将在每个周期内增加1。如果一个音频从输入设备中读取含有160个采样周期的块,那么对每个块,时间戳的值增加160,而不考虑该块是否用一个包传递或是被丢弃。

        时间戳的初始值应当是随机的,就像序号一样。几个连续的RTP包如果(逻辑上)是同时产生的,如:属于同一个视频帧的RTP包,将有相同的序列号。如果数据并不是以它采样的顺序进行传输,那么连续的RTP包可以包含不是单调递增(或递减)的时间戳(RTP包的序列号仍然是单调变化的)。

  
根据一些文章我自己推敲了一下几个概念如下:   
       时间戳单位:时间戳计算的单位不为秒之类的单位,而是由采样频率所代替的单位,这样做的目的就是为了是时间戳单位更为精准。比如说一个音频的采样频率为8000HZ,那么我们可以把时间戳单位设为1/8000。
       时间戳增量:相邻两个RTP包之间的时间差(以时间戳单位为基准)。
 
       如何设定时间戳之间的增量呢?
       按照刚才时间戳单位来看,1秒钟按照时间戳单位就是8000,那么一秒钟如果可以播放20帧,也就是发送30帧(帧率),那么可以求出相邻两帧之间的时间差,也就是时间戳增量,那么显而易见是用8000/20,那么这个时间戳增量就为400.
       网上大多数列举的一个例子是: 例如MPEG,每帧20ms,采样频率8000Hz,设定时间戳单位1/8000,而每个包之间就是160的增量
    这里又该如何理解呢?可以轻易地看出增量是直接8000与20ms相乘的结果,我们可以知道这里两帧之间的时间为20ms,也就是0.02s,这个单位是以秒来衡量的,那么我们要用时间戳单位来表示那么就是8000*0.02=160.所以时间戳增量为160.

        还有一点为什么一般都用90000作为视频采样频率呢?
        90k是用于视频同步的时间尺度(TimeScale),就是每秒90k个时钟tick。为什么采用90k呢?目前视频的帧速率主要有25fps、29.97fps、30fps等,而90k刚好是它们的倍数,所以就采用了90k。


     
         
posted @ 2010-01-25 16:16 Mr.Johnson 阅读(5581) | 评论 (0)编辑 收藏
  2009年12月3日
     摘要: FormatMessage函数 在《windows核心编程》中第一个示例便是它的使用。 这个函数我用了几次之后终于明白它的用法; 这个函数是用来格式化消息字符串,就是处理消息资源的。消息资源是由mc.exe 编译的,详细请在msdn中搜索mc.exe。 先来看下它的函数原型 DWORD WINAPI FormatMessage(  __in&...  阅读全文
posted @ 2009-12-03 04:25 Mr.Johnson 阅读(3313) | 评论 (1)编辑 收藏
  2009年11月27日
       在分流器那篇文章中说到窗口子类化什么的就造成了要使用不同的宏,我一直没看明白为什么说在对话框中要那样做.
原文地址
http://blog.csdn.net/hopkins9961629/archive/2006/01/25/588184.aspx
        原文不懂的地方如下:

以上四重,是消息分离器的基本使用,但,这不完整,消息分离器主要应用在对话框消息处理中。
这里,窗口子类化是我们经常使用的手段,这也可以通过消息分流器实现,

第五重
LRESULT CALLBACK Dlg_Proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
  HANDLE_MSG(hwnd, WM_INITDIALO , Cls_OnInitDialog); // 不能直接使用HANDLE_MSG宏
  HANDLE_MSG(hwnd, WM_COMMAND, Cls_OnCommand); // 不能直接使用HANDLE_MSG宏
 }
 return false;

由于是窗口子类化,所以,最后,返回的是false,以表明,如果没有约定响应的消息,
则返回父亲窗口false,如果有,则返回ture,这是与前四重不同的地方。
一般情况下,对话框过程函数应该在处理了消息的情况下返回TRUE,如果没有处理,则返回FALSE。
如果对话框过程返回了FALSE,那么对话框管理器为这条消息准备默认的对话操作。

但是,这其中有错误,因为有的消息,需要单独处理。单独处理的消息列表见SetDlgMsgResult宏。


  为什么不能使用?
          查阅msdn发现在对话框的过程函数和window窗口有个不一样地方是在返回值上面。如果消息被处理,那么它将返回true,而如果没有处理,那么将返回false,让对话框管理器(DialogBox Manager)处理。如果想有自己的返回值的话,那么使用子类化SetWindowLong函数中的DWL_MSGRESULT标识符改变,但是之后还是要返回true。但是有一些消息不遵守上诉约定,而直接返回他们的返回值,这些消息如下:

  • WM_CHARTOITEM
  • WM_COMPAREITEM
  • WM_CTLCOLORBTN
  • WM_CTLCOLORDLG
  • WM_CTLCOLOREDIT
  • WM_CTLCOLORLISTBOX
  • WM_CTLCOLORSCROLLBAR
  • WM_CTLCOLORSTATIC
  • WM_INITDIALOG
  • WM_QUERYDRAGICON
  • WM_VKEYTOITEM


    了解了这些之后看看那些定义的分流器宏
    消息处理宏:

    #define HANDLE_MSG(hwnd, message, fn)    \
        
    case (message): return HANDLE_##message((hwnd), (wParam), (lParam), (fn))


    WM_COMMAND宏的消息分流器定义:

    #define HANDLE_WM_COMMAND(hwnd, wParam, lParam, fn) \
        ((fn)((hwnd), (
    int)(LOWORD(wParam)), (HWND)(lParam), (UINT)HIWORD(wParam)), 0L)

            我们可以看到如果在对话框的过程函数中直接使用HANDLE_MSG的话,我们使用WM_COMMAND消息,那么根据HANDLE_WM_COMMAND宏,我们可以知道它会返回0L,0在windef.h中定义为false,那么我们明明处理这个wm_command消息,但是我们告诉对话框管理器是没有处理,让对话框管理器处理。所以如果这里直接使用HANDLE_MSG是不可行的,那么我们就使用一个宏改进它
    #define     SetDlgMsgResult(hwnd, msg, result) (( \
            (msg) 
    == WM_CTLCOLORMSGBOX      || \
            (msg) 
    == WM_CTLCOLOREDIT        || \
            (msg) 
    == WM_CTLCOLORLISTBOX     || \
            (msg) 
    == WM_CTLCOLORBTN         || \
            (msg) 
    == WM_CTLCOLORDLG         || \
            (msg) 
    == WM_CTLCOLORSCROLLBAR   || \
            (msg) 
    == WM_CTLCOLORSTATIC      || \
            (msg) 
    == WM_COMPAREITEM         || \
            (msg) 
    == WM_VKEYTOITEM          || \
            (msg) 
    == WM_CHARTOITEM          || \
            (msg) 
    == WM_QUERYDRAGICON       || \
            (msg) 
    == WM_INITDIALOG             \
        ) 
    ? (BOOL)(result) : (SetWindowLongPtr((hwnd), DWLP_MSGRESULT, (LPARAM)(LRESULT)(result)), TRUE))
    这样就可以解决这个问题了
    像Jeffrey Richter 高手自己也定义了一个宏:
    #define chHANDLE_DLGMSG(hWnd, message, fn)                 \
       
    case (message): return (SetDlgMsgResult(hWnd, uMsg,     \
          HANDLE_##message((hWnd), (wParam), (lParam), (fn))))
    这样可以更方便的在对话框过程函数中使用消息分流器了!
  • posted @ 2009-11-27 21:34 Mr.Johnson 阅读(414) | 评论 (0)编辑 收藏
    仅列出标题