English 中文(简体)
为什么可以将汽车用作模版的参数?
原标题:Why can t auto be used as a template type parameter?

I ve与C++0x sauto等关键词一起玩.,并尝试如下。

std::unique_ptr<auto> ptr(new int(0));

我尝试将其编成4.4.5英寸,并 go

error: invalid use of auto

视线判断auto 可轻易推断为<代码>int。

我的猜测是那种推论,模板发动机相互不谈。 否则,模板发动机将知道将模板类别以<代码>int作为参数。

另一gues是标准,我认为这是标准。

A member shall not be declared with auto, extern or register storage class.

但我认为,作为当地变量的<代码>auto<>/code>,而不是作为引出类型使用的<代码>auto。

我最后的猜测是,汇编者认为这是一种“<条码>auto的储存类别,而不是“条码>auto以进行类型的扣减。

标准中是否说明了这一点?

最佳回答

@dascandy正确地指出了与你的法典有什么错误。 我试图提出一些理由:

页: 1 因为这一论点是int*unique_ptr<int>。 有一个建筑商接受<代码>int*。 请允许我忽视以下事实:我们重新使用<代码>:unique_ptr,只是谈论我们所写的一个模板类别(并且可以专门化)。

编辑员为什么要引用<代码>unique_ptr<int>? 论点为t int, 它为int*。 为什么应该 t问:unique_ptr<int*>? 当然,这将产生汇编错误,因为unique_ptr<int*> s 构造者获得int*。 除非我增加一个专业:

template<>
class unique_ptr<int*>
{
public:
    unique_ptr(int*) {}
};

现在,unique_ptr<int*>将汇编成册。 汇编者应如何知道选择哪些,unique_ptr<int>unique_ptr<int*>? 如果我增加另一个专业?

template<>
class unique_ptr<double>
{
public:
    unique_ptr(int*) {}
};

The compiler now has three options to choose from, and it has to instantiate the template with every possible argument in order to find them. Clearly this is not feasible, especially with multiple template arguments and template recursion.

您可以做些什么是将所推断的类型与一个模版连接起来的工厂功能:

template<typename T>
std::unique_ptr<T> make_unique(T* arg) { return arg; }

(of course, this won t work because unique_ptr cannot be copied. But the idea is valid, and used in e.g.make_shared and make_pair.)


一些极端ug节的例子:

人们可以辩称,unique_ptr<jointd_ptr<int>>。 该法典有效。

或如何:

template<typename T>
class unique_ptr
{
public:
    explicit unique_ptr(T* arg);
    unique_ptr(int*, enable_if<(sizeof(T) > 16)>::type* = 0);
};
问题回答

这是因为,它必须确定哪类人来打造人之前<>?>? 如果你使施工人员成为模板,那么它就象任何其他模板功能一样——即自动引证论点。

Just want to add that a solution already exists for most cases:

template <typename T>
std::unique_ptr<T> unique_ptr_auto(T* ptr)
{
    // fails to handle std::unique_ptr<T[]>, not deducible from pointer
    return std::unique_ptr<T>(ptr);
}

auto ptr = unique_ptr_auto(new int(0));

很显然,人们会看到这样的想法。 这些“发电机功能”非常常见。

这一标准(或类似)<_>。 拟议的功能包括:

std::vector<int> GetMahVector();
std::vector<auto> var = GetMahVector();

然而,它遭到拒绝。 为什么遭到拒绝,你必须尽可能提出相关的标准程序文件。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?