Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获取向量中元素的类型?_C++_C++11_Vector_Stl - Fatal编程技术网

C++ 如何获取向量中元素的类型?

C++ 如何获取向量中元素的类型?,c++,c++11,vector,stl,C++,C++11,Vector,Stl,考虑模板元编程技术,如果我有 std::vector<int> v; v.push_back(42.42f); 到 foo(容器、元素…) 如果int是container元素的类型,我认为这也不安全,而且更冗长,更容易出错 那么如何在C++11中检索容器元素的类型呢?您可以得到如下类型: typename std::vector<T>::value_type; 然后 std::vector v; 特殊推回(v,3.14);//编译时错误:double不是int 如果

考虑模板元编程技术,如果我有

std::vector<int> v;
v.push_back(42.42f);

foo(容器、元素…)
如果
int
container
元素的类型,我认为这也不安全,而且更冗长,更容易出错


那么如何在C++11中检索容器元素的类型呢?

您可以得到如下类型:

typename std::vector<T>::value_type;
然后

std::vector v;
特殊推回(v,3.14);//编译时错误:double不是int

如果我正确理解了注释,您也可以尝试:

template <class Container, class... Args>
void foo(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;

    // use type_traits to check value_type
    ...
}
然后在以下情况下使用
std::enable_:

template <class Container, class... Args>
typename std::enable_if<has_value_type<Container>::value, return_type>::type 
foo(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;
    ...
}

如果您有C++11功能,还可以使用
decltype
关键字简化对底层
value\u type
类型成员的访问:

decltype(TheContainer)::value_type nVarOfType;

在这里,
容器
可以是任何类型的容器。例如,
map
deque
或任何其他STL容器-所有STL容器都定义了
value\u类型
类型
decltype
键将给出给定对象的类型。

也许您正在寻找std::vector::value\u类型?不,我有一个vector的实例,我有一个
container
,它表示一个已分配的容器,传递给
foo
的接口,我必须走另一条路。@user2485710我不确定是否遵循。我加了一个例子。我希望这能有所帮助。假设我有
foo
这个容器+元素,它基本上是一个可变模板。请注意,我在
foo
@user2485710的示例中使用了
:如果我理解正确,decltype(container)::value\u type应该可以工作。我试图以另一种方式实现某些功能,但可能比您在这里展示的更麻烦。我可能会像你在这里做的那样,将向量的类型添加到模板中,这样我可以简化它。
std::vector<int> v;
special_push_back(v, 3.14); // Compile time error: double is not int
template <class Container, class... Args>
void foo(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;

    // use type_traits to check value_type
    ...
}
template <class T>
struct has_value_type
{
private:
    template <class U> static std::false_type test(...);
    template <class U> static std::true_type test(typename U::value_type*);
public:
    enum { value = decltype(test<T>(0))::value };
};
template <class Container, class... Args>
typename std::enable_if<has_value_type<Container>::value, return_type>::type 
foo(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;
    ...
}
template <class Container, class... Args>
typename std::enable_if<
    has_value_type<Container>::value
    and std::is_same<int, typename Container::value_type>::value
>::type
foo2(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;

    // here value_type equals int
}
template <typename...>
struct all_of;

template <typename T>
struct all_of<T> : std::conditional<T::value == true,
    std::true_type, std::false_type>::type
{};

template <typename Head, typename... Tail>
struct all_of<Head, Tail...> : std::conditional<
    Head::value == true and all_of<Tail...>::value,
    std::true_type,
    std::false_type>::type
{};
`all_of<std::is_nothrow_copy_constructible<Args>...>::value`
template <class Container, class... Args>
typename std::enable_if<
    has_value_type<Container>::value
    and all_of<std::is_same<typename Container::value_type, Args>...>::value
>::type
foo2(Container&& c, Args&&... args) {
    typedef typename Container::value_type value_type;

    // here *all* types in Args are equal value_type
}
decltype(TheContainer)::value_type nVarOfType;