xcode 隐式模板参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4947294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:12:40  来源:igfitidea点击:

Implicit Template Parameters

c++xcodetemplatesimplicit

提问by Maxpm

The following code generates a compile error in Xcode:

以下代码在 Xcode 中生成编译错误:

template <typename T>
struct Foo
{
    Foo(T Value)
    {
    }
};

int main()
{
    Foo MyFoo(123);
    return 0;
}

error: missing template arguments before 'MyFoo'

error: missing template arguments before 'MyFoo'

Changing Foo MyFoo(123);to Foo<int> MyFoo(123);fixes the issue, but shouldn't the compiler be able to figure out the appropriate datatype?

更改Foo MyFoo(123);Foo<int> MyFoo(123);解决问题,但编译器不应该能够找出适当的数据类型吗?

Is this a compiler bug, or am I misunderstanding implicit template parameters?

这是编译器错误,还是我误解了隐式模板参数?

采纳答案by Jonathan Grynspan

The constructor could in theory infer the type of the object it is constructing, but the statement:

构造函数理论上可以推断出它正在构造的对象的类型,但是语句:

Foo MyFoo(123);

Is allocating temporary space for MyFooand must know the fully-qualified type of MyFooin order to know how much space is needed.

正在为 分配临时空间,MyFoo并且必须知道 的完全限定类型MyFoo才能知道需要多少空间。

If you want to avoid typing (i.e. with fingers) the name of a particularly complex template, consider using a typedef:

如果您想避免输入(即用手指)特别复杂模板的名称,请考虑使用typedef

typedef std::map<int, std::string> StringMap;

Or in C++0x you could use the autokeyword to have the compiler use type inference--though many will argue that leads to less readable and more error-prone code, myself among them. ;p

或者在 C++0x 中,您可以使用auto关键字让编译器使用类型推断——尽管许多人会争辩说这会导致代码可读性降低且更容易出错,其中包括我自己。;p

回答by Andriy Tylychko

compiler can figure out template parameter type only for templated functions, not for classes/structs

编译器只能为模板化函数确定模板参数类型,不能为类/结构确定模板参数类型

回答by Palmik

It's not a bug, it's non-existing feature. You have to fully specify class/structure template arguments during instantiation, always, the types are not inferred as they can be for function templates.

这不是错误,而是不存在的功能。您必须在实例化期间完全指定类/结构模板参数,始终不会像函数模板那样推断类型。

回答by Nawaz

Compiler can deduce the template argument such case:

编译器可以推导出模板参数这种情况:

template<typename T>
void fun(T param)
{
    //code...
}

fun(100);    //T is deduced as int;
fun(100.0);  //T is deduced as double
fun(100.0f); //T is deduced as float

Foo<int> foo(100);
fun(foo);    //T is deduced as Foo<int>;

Foo<char> bar('A');
fun(bar);    //T is deduced as Foo<char>;

Actually template argument deduction is a huge topic. Read this article at ACCU:

实际上模板参数推导是一个巨大的话题。在 ACCU 阅读这篇文章:

The C++ Template Argument Deduction

C++ 模板参数推导

回答by Riot

In C++11 you can use decltype:

在 C++11 中,您可以使用decltype

int myint = 123;
Foo<decltype(myint)> MyFoo(myint);

回答by Andrew Coggin

What you are trying to do now works in C++ 17. Template parameters can be inferred in C++ 17.

您现在尝试执行的操作在 C++ 17 中有效。可以在 C++ 17 中推断模板参数。

template <typename T>
struct Foo
{
    Foo(T Value)
    {
    }
};

int main()
{
    Foo a(123);
    Foo b = 123;
    Foo c {123};
    return 0;
}

回答by CashCow

It makes a lot of sense it is like this, as Foo is not a class, only Foo<T>where T is a type.

像这样很有意义,因为 Foo 不是一个类,只有Foo<T>T 是一个类型。

In C++0x you can use auto, and you can create a function to make you a Foo, let's call it foo (lower case f). Then you would do

在 C++0x 中,您可以使用 auto,并且您可以创建一个函数来使您成为 Foo,我们称之为 foo(小写 f)。那么你会做

template<typename T> Foo<T> foo(int x)
{
  return Foo<T>(x);
}

auto myFoo = foo(55);