64,654
社区成员
发帖
与我相关
我的任务
分享
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;
}
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>);
}
class A
{
A(){};
template<int>
A(){};
}
A a; // 此时,怎样才能调用template<int>A(){};
[/quote]
构造函数的模板参数好像没办法显示调用
等 @fefe82 大神解释一下 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"; };
估计这也是你的本意。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 ;
}
#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;
}
class A
{
A(){};
template<int>
A(){};
}
A a; // 此时,怎样才能调用template<int>A(){};