构造函数为模板时,无法调用

GKatHere 2017-02-11 11:19:46
如题: 构造函数为模板时,无法调用

enum CTForWCARR{CTForWCARR_v};
template<class ARRElement_, size_t ARRSize_>
struct A :public std::array<ARRElement_, ARRSize_>
{
typedef ARRElement_ ARRElement;
static const size_t ARRSize =ARRSize_;

A(){std::cout <<__LINE__ << "\n";};


//template<CTForWCARR>
//A(){std::cout <<__LINE__ << "\n";}; // warning C4520: “A<ARRElement_,ARRSize_>”: 指定了多个默认构造函数

template<CTForWCARR ct>
A(CTForWCARR t){std::cout <<__LINE__ << "\n";};
};

int _tmain(int argc, _TCHAR* argv[])
{

A<int ,10> a;
A<wchar_t ,10> b;

//A<wchar_t ,10><A<wchar_t ,10>::CTForWCARR_v> b;
A<wchar_t ,10> c(CTForWCARR_v); // ERROR: 不能将参数 1 从“CTForWCARR”转换为“const A<ARRElement_,ARRSize_> &”

getchar();
return 0;
}




...全文
314 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lunat 2017-02-13
  • 打赏
  • 举报
回复
引用 12 楼 lunat 的回复:
构造函数的模板参数只能靠推导,无法显式指定,不过可以曲线救国:

class A{
public:
    template<class T> struct tag{ using type = T; };
    template<class T>
    A(tag<T>) {}
};

void foo() {
      A a(tag<int>);
      A b(tag<float>);
}
struct tag;
lunat 2017-02-13
  • 打赏
  • 举报
回复
构造函数的模板参数只能靠推导,无法显式指定,不过可以曲线救国:

class A{
public:
    template<class T> tag{ using type = T; };
    template<class T>
    A(tag<T>) {}
};

void foo() {
      A a(tag<int>);
      A b(tag<float>);
}
ri_aje 2017-02-13
  • 打赏
  • 举报
回复
构造函数的模版参数没法显示指定,如果推导不出来,那就是编译错误了。
yshuise 2017-02-12
  • 打赏
  • 举报
回复
我是用编译器调试通过的。
paschen 版主 2017-02-12
  • 打赏
  • 举报
回复
引用 3 楼 GKatHere的回复:
[quote=引用 1 楼 paschen 的回复:] 删掉template<CTForWCARR>这一部分
我指

class A
{
 A(){};
template<int>
A(){};
}
A a;   // 此时,怎样才能调用template<int>A(){};
[/quote] 构造函数的模板参数好像没办法显示调用 等 @fefe82 大神解释一下
fesdobat 2017-02-12
  • 打赏
  • 举报
回复
问题在
   template<CTForWCARR ct>
    A(CTForWCARR t){std::cout <<__LINE__ << "\n";};
这里,固定类型CTForWCARR 做模板参数是无法自动推导的,只能手动指定。 你的调用要这么写:
    A<wchar_t ,10>  c.A<CTForWCARR_v>(CTForWCARR_v);
但是C++构造函数是不让显式调用的,所以上面的编译不通过,我们也没办法手动指定 构造函数的模板参数。 如果你只是想让构造函数根据参数类型自动推导生成,你得这么改写构造函数:
	template<class ct>
	A(ct t) { std::cout << __LINE__ << "\n"; };
估计这也是你的本意。
ID870177103 2017-02-12
  • 打赏
  • 举报
回复
函数的签名(名字+类型)不能一致,否则编译器无法区分它们 需要引入‘占位符’来区分
template <int>
struct placeholder {} ;

static constexpr placeholder<0> ph0 ;
static constexpr placeholder<1> ph1 ;
static constexpr placeholder<2> ph2 ;

struct A {
	A () = default ;

	template <int UUID>
	A (const placeholder<UUID> &) {}
} ;

int main () {
	A a1 ;
	A a2 (ph0) ;
	A a3 (ph1) ;
	return 0 ;
}
paschen 版主 2017-02-11
  • 打赏
  • 举报
回复
删掉template<CTForWCARR>这一部分
GKatHere 2017-02-11
  • 打赏
  • 举报
回复
引用 5 楼 yshuise 的回复:
#include <iostream>
#include <array>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
enum CTForWCARR{CTForWCARR_v};
template<class ARRElement_,    size_t ARRSize_>
struct A    :public std::array<ARRElement_, ARRSize_>
{
typedef ARRElement_ ARRElement;
static const  size_t    ARRSize =ARRSize_;
 
    A(){std::cout <<__LINE__ << "\n";};
 
 
    //template<CTForWCARR>
    //A(){std::cout <<__LINE__ << "\n";};        //    warning C4520: “A<ARRElement_,ARRSize_>”: 指定了多个默认构造函数
 
    template<CTForWCARR ct,typename C>
    A(CTForWCARR t){std::cout <<__LINE__ << "\n";};       
};
int main(int argc, char** argv) {
	
	 A<int ,10> a;
    A<wchar_t ,10> b;
     
    //A<wchar_t ,10><A<wchar_t ,10>::CTForWCARR_v> b;
    A<wchar_t ,10> c(CTForWCARR);      
	return 0;
}
A<wchar_t ,10> c(CTForWCARR); 错了。这是个函数
yshuise 2017-02-11
  • 打赏
  • 举报
回复
#include <iostream>
#include <array>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
enum CTForWCARR{CTForWCARR_v};
template<class ARRElement_,    size_t ARRSize_>
struct A    :public std::array<ARRElement_, ARRSize_>
{
typedef ARRElement_ ARRElement;
static const  size_t    ARRSize =ARRSize_;
 
    A(){std::cout <<__LINE__ << "\n";};
 
 
    //template<CTForWCARR>
    //A(){std::cout <<__LINE__ << "\n";};        //    warning C4520: “A<ARRElement_,ARRSize_>”: 指定了多个默认构造函数
 
    template<CTForWCARR ct,typename C>
    A(CTForWCARR t){std::cout <<__LINE__ << "\n";};       
};
int main(int argc, char** argv) {
	
	 A<int ,10> a;
    A<wchar_t ,10> b;
     
    //A<wchar_t ,10><A<wchar_t ,10>::CTForWCARR_v> b;
    A<wchar_t ,10> c(CTForWCARR);      
	return 0;
}
GKatHere 2017-02-11
  • 打赏
  • 举报
回复
引用 2 楼 yshuise 的回复:
template<CTForWCARR ct> A(CTForWCARR t){std::cout <<__LINE__ << "\n";}; ================ 少一个模板参数
... 怎么用
GKatHere 2017-02-11
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
删掉template<CTForWCARR>这一部分
我指

class A
{
 A(){};
template<int>
A(){};
}
A a;   // 此时,怎样才能调用template<int>A(){};
yshuise 2017-02-11
  • 打赏
  • 举报
回复
template<CTForWCARR ct> A(CTForWCARR t){std::cout <<__LINE__ << "\n";}; ================ 少一个模板参数

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧