我想强制使用可变参数模板的类型与先前设置的模板类型相同.在下面的例子中,我希望T和U是相同的类型.
#include <iostream>
#include <string>
template<class T>
struct Foo {
Foo(T val) {
std::cout << "Called single argument ctor" << std::endl;
// [...]
}
// How to enforce U to be the same type as T?
template<class... U>
Foo(T first, U... vals) {
std::cout << "Called multiple argument ctor" << std::endl;
// [...]
}
};
int main() {
// Should work as expected.
Foo<int> single(1);
// Should work as expected.
Foo<int> multiple(1, 2, 3, 4, 5);
// Should't work …Run Code Online (Sandbox Code Playgroud) 我需要确定给定的对象是否是Array, 或类型化数组,例如Float32Array.
目前我正在检查该.length属性是否已定义,但这并不总是表示数组。或其他方法的存在性检查也会出现类似的问题.forEach()。
几次instanceof检查就足够了,就像这里所做的那样- 但我想知道是否有一个简单的内置功能,例如,一个通用Array.isArray()函数可以满足我的需要。