Is there really an explicit specialization on a template with auto-return of 'type' in C ++ 14?

The previous question .

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

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

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

3). Why is the code below compiled and works (sometimes) when using C ++ 11 compilers? Alternatively, why does this not always work? Are there any specific flags / options / settings that may interfere with the work?

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"));
}
+4
source share
1 answer

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

Yes, as far as I can tell. This is sometimes difficult to prove, because often it simply prohibits nothing. However, we can see an example in a recent project (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

The same paragraph shall indicate:

, , , .

, . , . , ++ 98 ( ) , . , , , .


3). , , () ++ 11?

OP ++ 11. (non-lamdas) - , ++ 14. , , . , () . [intro.compliance]/2.2:

[...], .

/8

( ) , . , , . , , .

( , .)

g++ 4.8.3 , . g++ 4.9 , . . -Werror, g++ 4.8.3, . ( gcc, .)

+6

Source: https://habr.com/ru/post/1568828/


All Articles