...不是函数模板的特化 [英] ...is not a specialization of a function template

查看:100
本文介绍了...不是函数模板的特化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译以下代码时,我收到有关专业化的错误。



  #include   <  算法 >  
#include < time.h >
#include < < span class =code-leadattribute> stdlib.h >
#include < stdio.h < span class =code-keyword>>
#in clude < iostream >

使用 命名空间标准;

class MyRandom {
public
MyRandom (){
srand(time( 0 ));
}

int operator ()() const {
return rand()% 10 ;
}
};

模板< typename T>
class MyPrint {
public
void operator ()(T const& v) const {
cout<< v<< ENDL;
}

模板<>
void operator ()( int const& v) const {
cout<< int:<< v<< ENDL;
}
};


int main(){
int 元素[ 10 ];

generate(elements,elements + 10 ,MyRandom());

for_each(元素,元素+ 10 ,MyPrint< int>());

return 0 ;
}





我不明白,对我来说似乎没问题:(

解决方案

尝试:

  #include   <  算法 >  
#include < time.h >
#include < stdlib.h >
#include < stdio.h >
#include < iostream >

使用 命名空间标准;

class MyRandom {
public
MyRandom (){
srand(time( 0 ));
}

int operator ()() const {
return rand()% 10 ;
}
};

模板< typename T>
class MyPrint {
public
void operator ()(T const& v) const {
cout<< v<< ENDL;
}
};

模板<> void MyPrint< int> :: operator ()( int const & v) const {cout<< int:<< v<< endl;}


int main(){
int elements [ 10 ];

generate(elements,elements + 10 ,MyRandom());

for_each(元素,元素+ 10 ,MyPrint< int>());

return 0 ;
}





 


模板化类的成员函数不能脱掉类模板参数,这似乎是你的意图。事实上,编译器将成员函数视为给定类模板参数的模板化类的特定实例化中的非模板化函数。例如,如果您从未实例化calss模板,编译器甚至不会注意到,但如果这样做,将使用传递给类模板实例化的参数,并且不能成为成员函数的自由模板参数。



E. g。如果您有以下代码:

 模板< class t> 
class myT {
T var;
public
void hello()常量;
};
void foo {
myT< int> X;
x.hello();
}



然后函数hello将立即显示为 myT< int> :: hello()


When I try to compile the following code I get an error about the specialization.

#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class MyRandom {
public:
	MyRandom() {
		srand(time(0));
	}

	int operator() () const {
		return rand() % 10;
	}
};

template<typename T>
class MyPrint {
public:
	void operator() (T const& v) const {
		cout << v << endl;
	}

	template<>
	void operator() (int const& v) const {
		cout << "int:" << v << endl;
	}
};


int main() {
	int elements[10];

	generate(elements, elements + 10, MyRandom());

	for_each(elements, elements + 10, MyPrint<int>());

	return 0;
}



I don't get it, it seems ok to me :(

解决方案

Try:

#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class MyRandom {
public:
  MyRandom() {
    srand(time(0));
  }

  int operator() () const {
    return rand() % 10;
  }
};

template<typename T>
class MyPrint {
public:
  void operator() (T const& v) const {
    cout << v << endl;
  }
};

template <> void MyPrint<int>::operator()(int const &v) const { cout << "int: " << v << endl;}


int main() {
  int elements[10];

  generate(elements, elements + 10, MyRandom());

  for_each(elements, elements + 10, MyPrint<int>());

  return 0;
}




The member function of a templated class cannot shed the class template parameter, which appears to be your intention. The compiler in fact treats the member function as a non-templated function within the specific instantiation of the templated class for a given class template argument. I. e., if you never instantiate the calss template, the compiler won't even notice, but if you do, the argument passed to the class template instantiation will be used and can't be a free template argument for member functions.

E. g. if you have the following code:

template <class t>
class myT {
   T var;
public:
   void hello() const;
};
void foo {
   myT<int> x;
   x.hello();
}


Then the function hello will be instantated as myT<int>::hello().


这篇关于...不是函数模板的特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆