C++ 如何将对象数组作为参数传递给模板
我正在尝试创建一个模板,该模板将接受对对象的C样式数组的引用作为参数:C++ 如何将对象数组作为参数传递给模板,c++,arrays,c++11,templates,C++,Arrays,C++11,Templates,我正在尝试创建一个模板,该模板将接受对对象的C样式数组的引用作为参数: #include <iostream> class A { public: A(){} }; template<int N, class A& obj> struct W {}; int main() { A b[5]; W<5,b> w; } #包括 甲级 { 公众: A(){} }; 模板结构W{}; int main() { A b[5]; W; } 但在编译代
#include <iostream>
class A
{
public:
A(){}
};
template<int N, class A& obj> struct W {};
int main()
{
A b[5];
W<5,b> w;
}
#包括
甲级
{
公众:
A(){}
};
模板结构W{};
int main()
{
A b[5];
W;
}
但在编译代码时,我得到了一个错误:
$ c++ -std=c++11 -g try37.cpp
try37.cpp: In function 'int main()':
try37.cpp:14:5: error: the value of 'b' is not usable in a constant expression
W<5,b> w;
^
try37.cpp:13:3: note: 'b' was not declared 'constexpr'
A b[5];
^
try37.cpp:14:6: error: could not convert template argument 'b' to 'A&'
W<5,b> w;
^
try37.cpp:14:9: error: invalid type in declaration before ';' token
W<5,b> w;
^
<代码> $C++ -STD= C++ 11 -G Try37.CPP
try37.cpp:在函数“int main()”中:
try37.cpp:14:5:错误:“b”的值在常量表达式中不可用
W;
^
try37.cpp:13:3:注意:“b”未声明为“constexpr”
A b[5];
^
try37.cpp:14:6:错误:无法将模板参数“b”转换为“A&”
W;
^
try37.cpp:14:9:错误:在“;”之前的声明中的类型无效代币
W;
^
我尝试了许多方法,但无法解决编译问题?如何解决相同问题?首先,您只能将一个实例传递给模板。 将其更改为
A[n]obj
注意:它将被转换为指向的指针
其次,传递的阵列必须具有静态存储持续时间。因此,要么将数组设为静态数组,要么将其设为全局变量。首先,您只能向模板传递一个实例。 将其更改为
A[n]obj
注意:它将被转换为指向的指针
其次,传递的阵列必须具有静态存储持续时间。因此,要么将数组设为静态数组,要么将其设为全局变量。编辑:答案中添加了C样式的数组 如果您试图让struct W拥有一个对象容器,您可以使用向量,但是。我还是不知道你为什么要这么做
struct W
{
template<typename Type, typename A> //constructor with a vector
W(std::vector<Type,A> & vec)
{
//...
}
template<typename Type>
W(int arraySize, Type & obj) //constructor with an array
{
//...
}
};
int main()
{
const int ArraySize = 5;
A b[ArraySize];
std::vector<A> vec;
for(int i =0; i < 5; i++)
vec.push_back(b[i]);
W w(vec); //calling struct W constructor that takes a vector.
W w2(ArraySize,b); //calling struct W constructor that takes c style array
return 0;
}
struct W
{
模板//带向量的构造函数
W(标准::向量和向量)
{
//...
}
模板
W(int-arraySize,Type&obj)//带数组的构造函数
{
//...
}
};
int main()
{
常数int ArraySize=5;
A b[排列];
std::vec;
对于(int i=0;i<5;i++)
向量推回(b[i]);
W(vec);//调用接受向量的struct W构造函数。
ww2(ArraySize,b);//调用采用c样式数组的struct W构造函数
返回0;
}
编辑:答案中添加了C样式的数组
如果您试图让struct W拥有一个对象容器,您可以使用向量,但是。我还是不知道你为什么要这么做
struct W
{
template<typename Type, typename A> //constructor with a vector
W(std::vector<Type,A> & vec)
{
//...
}
template<typename Type>
W(int arraySize, Type & obj) //constructor with an array
{
//...
}
};
int main()
{
const int ArraySize = 5;
A b[ArraySize];
std::vector<A> vec;
for(int i =0; i < 5; i++)
vec.push_back(b[i]);
W w(vec); //calling struct W constructor that takes a vector.
W w2(ArraySize,b); //calling struct W constructor that takes c style array
return 0;
}
struct W
{
模板//带向量的构造函数
W(标准::向量和向量)
{
//...
}
模板
W(int-arraySize,Type&obj)//带数组的构造函数
{
//...
}
};
int main()
{
常数int ArraySize=5;
A b[排列];
std::vec;
对于(int i=0;i<5;i++)
向量推回(b[i]);
W(vec);//调用接受向量的struct W构造函数。
ww2(ArraySize,b);//调用采用c样式数组的struct W构造函数
返回0;
}
您的代码中存在一些问题
(1) 如果要将对象的引用作为模板参数传递,则必须将其定义为constepr
,并为其提供外部static
链接(static
,无需birdfreeyahoo的更正(谢谢!)),因此
(3) 如果希望W
的第二个模板参数是对a
s的常量C样式数组的引用,其中维度是第一个参数,则语法为
template <int N, A const (& obj)[N]>
struct W
{ };
模板
结构W
{ };
因此,完整的程序成为
class A
{
public:
constexpr A ()
{ }
};
template <int N, A const (& obj)[N]>
struct W
{ };
constexpr A b[5];
int main ()
{
W<5, b> w;
}
A类
{
公众:
constexpr A()
{ }
};
模板
结构W
{ };
consteprab[5];
int main()
{
W;
}
您的代码中存在一些问题
(1) 如果要将对象的引用作为模板参数传递,则必须将其定义为constepr
,并为其提供外部static
链接(static
,无需birdfreeyahoo的更正(谢谢!)),因此
(3) 如果希望W
的第二个模板参数是对a
s的常量C样式数组的引用,其中维度是第一个参数,则语法为
template <int N, A const (& obj)[N]>
struct W
{ };
模板
结构W
{ };
因此,完整的程序成为
class A
{
public:
constexpr A ()
{ }
};
template <int N, A const (& obj)[N]>
struct W
{ };
constexpr A b[5];
int main ()
{
W<5, b> w;
}
A类
{
公众:
constexpr A()
{ }
};
模板
结构W
{ };
consteprab[5];
int main()
{
W;
}
简单地使用std::array
而不是原始数组怎么样?还要注意,模板参数指的是指定类型,而不是对象。你能提供一个例子吗?它不能解决编译问题。也许你可以告诉我们你想要实现什么(在我看来这似乎是一个XY问题),我认为你不需要那种模板。简单地使用std::array
而不是原始数组怎么样?还要注意,模板参数指的是指定类型,而不是对象。你能提供一个例子吗?它不能解决编译问题。也许你可以告诉我们你想要实现什么(在我看来这是一个XY问题),我认为你不需要那种模板谢谢-但是我如何才能让它接受一个非全局变量?让它成为静态的,但是它就像一个只能在本地访问的全局变量(但每次函数调用都保持不变)。如果你想要一个真正的局部变量,你就不能,因为指针需要是一个constexpr,而这只能在编译时知道地址的情况下才能实现(所以静态存储持续时间)。这就是模板的工作原理。谢谢-但是我怎样才能让它接受一个非全局变量呢?让它成为静态的,但它就像一个全局变量一样,只能在本地访问(但每次函数调用都保持不变)。如果你想有一个真正的局部变量,你就不能,因为指针需要是一个constexpr,只有在编译时知道地址(因此静态存储持续时间)时,才会出现这种情况。这就是模板的工作原理。我的要求是,我需要创建一个规范模板
char arr[] = "abcdef;
sizeof_array(arr); // C-styled array
sizeof_array2(arr);