C ++:与通用const指针作斗争 [英] C++ : struggle with generic const pointer

查看:57
本文介绍了C ++:与通用const指针作斗争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些模板代码中,我遇到了一些关于const正确性的烦人问题,最终归结为以下观察:由于某种原因,给定STL-ish容器类型T, const typename即使 T :: pointer 等效于,T :: pointer 似乎实际上并没有提供恒定的指针类型。 T :: value_type *

I've run into some annoying issues with const-correctness in some templated code, that ultimately boils down to the following observation: for some reason, given an STL-ish Container type T, const typename T::pointer does not actually seem to yeild a constant pointer type, even if T::pointer is equivalent to T::value_type*.

以下示例说明了该问题。假设您有一个采用容器的模板化函数,该容器必须满足STL随机访问容器的概念要求。

The following example illustrates the problem. Suppose you have a templated function that takes a Container which must meet the STL Random Access Container concept requirements.

template <class Container>
void example(Container& c)
{
    const typename Container::pointer p1 = &c[0]; // Error if c is const
    const typename Container::value_type* p2 = &c[0]; 
}

然后,如果我们将此函数传递给const容器...

Then, if we pass this function a const container...

const std::vector<int> vec(10);
example(vec);

...我们从 const int * int * 。但是为什么在此示例中 const类型名Container :: pointer const int * 不同?

...we get an invalid conversion from const int* to int*. But why is const typename Container::pointer not the same as const int* in this example?

请注意,如果我将 const typename Container :: pointer 更改为简单的 typename Container :: const_pointer 它可以很好地编译,但是,据我所知,const_pointer typedef是一个扩展,(我没有在C ++标准容器要求(23.5,表65)中提到它),因此我不想使用它。

Note that if I change const typename Container::pointer to simply typename Container::const_pointer it compiles fine, however, as far as I can tell, the const_pointer typedef is an extension, (I don't see it mentioned in the C++ standard Container Requirements (23.5, Table 65)), and so therefore I don't want to use it.

那么我如何从容器T获取一个通用的,const正确的指针类型? (如果不使用boost :: mpl :: if_和type_traits来检查容器是否恒定,我真的看不到该怎么做……但是必须有一个更简单的方法来实现)

So how can I obtain a generic, const-correct pointer type from a container T? (I really can't see how to do this without using boost::mpl::if_ along with type_traits to check if the container is constant...but there must be a less verbose way to do this)

编辑:如果有关系,我将使用gcc 4.3.2进行编译。

In case it matters, I'm using gcc 4.3.2 to compile this.

推荐答案

它不起作用,因为您的 const 不适用于您认为的对象。例如,如果您有

It doesn't work because your const does not apply to what you think it applies to. For example, if you have

typedef int* IntPtr;

然后

const IntPtr p;

不代表

const int* p;

,而是代表

int* const p;

Typedef-name不是宏。将类型的指针包装成typedef名称后,就无法再使用它来创建指向const的指针了。即绝对没有办法使用上面的 IntPtr typedef-name来产生等价的

Typedef-name is not a macro. Once the "pointerness" of the type is wrapped into a typedef-name, there's no way to use it to create a pointer-to-const type anymore. I.e. there's absolutely no way to use the above IntPtr typedef-name to produce an equivalent of

const int* p;

您必须使用显式的pointe类型(就像对 value_type所做的那样) ),或检查您的容器是否定义了不同的typedef名称,其中 const 已经包装在内部(例如 const_pointer 或类似的东西)。

You have to either use to pointee type explicitly (as you did with value_type), or check whether your container defines a different typedef-name, with const already wrapped "inside" (like const_pointer or something like that).

这篇关于C ++:与通用const指针作斗争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆