在C ++ 14中使用自动返回“类型"进行显式模板专业化是否有效? [英] Is it valid to do explicit template specialisation with auto return 'type' in C++14?

查看:101
本文介绍了在C ++ 14中使用自动返回“类型"进行显式模板专业化是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题.

我重复上一个问题的代码,以使该问题独立.下面的代码如果使用gcc 4.8.3进行编译,则不会编译并发出任何警告.用-std=c++1y.但是,如果使用-std=c++0x标志进行编译,它会发出警告.在上一个问题的上下文中,有人指出该代码无法使用gcc 4.9.0进行编译.不幸的是,目前我还不完全了解auto的实现方式.因此,如果有人可以回答以下问题,我将不胜感激:

I repeat the code from the previous question to make this question self-contained. The code below compiles and does not issue any warnings if it is compiled using gcc 4.8.3. with -std=c++1y. However, it does issue warnings if compiled with -std=c++0x flag. In the context of the previous question it was stated that the code does not compile using gcc 4.9.0. Unfortunately, at present, I do not fully understand how auto is implemented. Thus, I would appreciate if anyone could answer the following questions:

1).下面的代码相对于C ++ 14标准是否有效?

1). Is the code below valid C++ with respect to the C++14 standard?

2).如果是,此代码是否被认为是一种好的样式?如果没有,为什么不呢?

2). If yes, would this code be considered a good style? If not, why not?

3).为什么在使用C ++ 11编译器时,下面的代码为什么可以编译并正常工作(有时)?或者,为什么它并不总是有效?是否有任何特定的标志/选项/设置可能会阻止其工作?

3). Why does the code below compile and work (sometimes) when using C++11 compilers? Alternatively, why does it not always work? Are there any specific flags/options/settings that could prevent it from working?

template<int N> auto getOutputPort2();
template<> auto getOutputPort2<0>();
template<> auto getOutputPort2<1>();

template<>
auto getOutputPort2<0>()
{
    return std::unique_ptr<int>(new int(10));
}

template<>
auto getOutputPort2<1>()
{
    return std::unique_ptr<string>(new string("qwerty"));
}

推荐答案

1).下面的代码相对于C ++ 14标准是否有效?

1). Is the code below valid C++ with respect to the C++14 standard?

是的,据我所知.有时很难证明,因为通常没有什么可禁止的.但是,我们可以在[dcl.spec.auto]/13的最新草案(N4296后)中查看一个示例:

Yes, as far as I can tell. It's sometimes a bit hard to prove, since often there's simply nothing forbidding it. However, we can look at an example in a recent draft (post-N4296), [dcl.spec.auto]/13:

template <typename T> auto g(T t) { return t; } // #1
template auto g(int);                           // OK, return type is int
template char g(char);                          // error, no matching template
template<> auto g(double);                      // OK, forward declaration with
                                                // unknown return type

同一段说明:

具有声明的返回类型且使用占位符类型的函数或函数模板的声明或特化也应使用该占位符,而不是推导类型.

Redeclarations or specializations of a function or function template with a declared return type that uses a placeholder type shall also use that placeholder, not a deduced type.

因此,函数模板的显式专业化必须使用返回类型推导. 我找不到任何禁止针对不同专业的不同返回类型的东西.类似地,在C ++ 98中,可以通过使返回类型依赖于模板参数来实现(相同主模板的)功能模板专业化的不同返回类型.通过使用元编程,您基本上可以达到与使用返回类型推导为不同的专业指定不相关的返回类型时相同的功能.

So the explicit specializations of a function template must use return type deduction. I cannot find anything that forbids different return types for different specializations. Similarly, in C++98, different return types for function template specializations (of the same primary template) can be achieved by making the return type dependent on the template arguments. By using metaprogramming, you can basically achieve the same as when using return type deduction to specify unrelated return types for the different specializations.

3).为什么下面的代码似乎可以使用C ++ 11编译器进行编译和运行(有时)?

3). Why does the code below seem to compile and work (sometimes) using C++11 compilers?

OP中的代码在C ++ 11中格式错误.普通函数(非ladas)的返回类型推导是C ++ 14中引入的功能.包含此代码段的程序是格式错误的.但是,该标准并未要求实现(编译器)必须拒绝格式错误的程序.它仅在[intro.compliance]/2.2中声明:

The code in the OP is ill-formed in C++11. Return type deduction for ordinary functions (non-lamdas) is a feature introduced in C++14. A program that contains this code snippet is ill-formed. However, the Standard does not mandate that implementations (compilers) must reject ill-formed programs. It merely states in [intro.compliance]/2.2:

如果程序包含违反任何可诊断规则的行为,则符合规范的实现应至少发布一条诊断消息.

If a program contains a violation of any diagnosable rule [...] a conforming implementation shall issue at least one diagnostic message.

和/8

一个符合标准的实现可以具有扩展(包括附加的库函数),前提是它们不会改变任何格式正确的程序的行为.需要实施以诊断使用了根据本国际标准格式不正确的此类扩展程序的程序.但是,这样做后,他们可以编译和执行此类程序.

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.

(因此实现可以接受此程序作为扩展.)

(So implementations can accept this program as an extension.)

g ++ 4.8.3发出警告,该警告被视为诊断消息. g ++ 4.9发出错误,这也是一条诊断消息.两者都符合.通过指定-Werror,您可以告诉g ++ 4.8.3拒绝该程序. (您必须询问gcc开发人员,为什么他们将其从警告更改为错误.)

g++4.8.3 issues a warning, which counts as a diagnostic message. g++4.9 issues an error, which is also a diagnostic message. Both are compliant. By specifying -Werror, you could tell a g++4.8.3 to reject this program. (You'd have to ask the gcc developers why they've changed that from a warning to an error.)

这篇关于在C ++ 14中使用自动返回“类型"进行显式模板专业化是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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