Use of 'auto func(int)' before deduction of 'auto' in C++14

when to use a semicolon
when to use a colon
use of in a sentence
when to use a semicolon examples
when to use a dash
use of semicolon in american english
when to use a comma
when to use a semicolon in a list

I have compiled following program in GCC using C++14.

#include <iostream>
using namespace std;

auto func(int i);

int main() 
{
    auto ret = func(5);
    return 0;
}

auto func(int i) 
{
  if (i == 1)
    return i;              
  else
    return func(i-1) + i;  
}

But, I get the following error.

In function 'int main()': 8:16: error: use of 'auto func(int)' before
deduction of 'auto'
 auto ret = func(5);

So, what am I missing here?


This is [dcl.spec.auto/11]:

If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression, the program is ill-formed. Once a non-discarded return statement has been seen in a function, however, the return type deduced from that statement can be used in the rest of the function, including in other return statements. [ Example:

auto n = n;                     // error, n's type is unknown
auto f();
void g() { &f; }                // error, f's return type is unknown
auto sum(int i) {
  if (i == 1)
    return i;                   // sum's return type is int
  else
    return sum(i-1)+i;          // OK, sum's return type has been deduced
}

 — end example ]

To translate this into English: the compiler needs to know the return type before you can use the function. In case of auto used like this, this is typically achieved by moving the definition before the point of use. If you don't actually need to use return type deduction, you can keep the definition after the use if you provide a signature, including the return type, in the declaration.

Semicolons: A Quick Guide How to Use a Semicolon, Semicolons are as basic as a period stacked on top of a comma. Does that mean you can use it like either one? Here are the rules for using semicolons correctly� 1 : to put into action or service : make use of use tools use good English. 2 —used with to to show a former custom, fact, or state Grandma said winters used to be harder. 3 : to take into the body people who use drugs I don't use sugar in tea. 4 : to do something by means of Be sure to use care.


Clang has a much better error message for that one:

main.cpp:8:16: error: function 'func' with deduced return type cannot be used before it is defined
    auto ret = func(5);
               ^

I guess that's self-explanatory.

Colon, Grammatical uses of the colon. Introducing a list. The colon is used to introduce a list of items. Example. The bookstore specializes in three subjects: art,� But remember, I said "of" isn't always wrong. There are good ways to use the word "of." For example, "Please bring me a bucket of water." You have to write it that way to show that you want a bucket that actually has water in it.


When auto is used as the return type in a function declaration that does not use the trailing return type syntax, the keyword auto indicates that the return type will be deduced from the operand of its return statement. That means the deduction can't be performed until the definition of the function func(), but before that it has been used in main().

You could move the definition before main(), or use trailing return type syntax to specify the return type on the declaration.

Using Semicolons – The Writing Center – UW–Madison, Use a semicolon between items in a list or series if any of the items contain commas. There are basically two ways to write: with a pen or pencil, which is� How to Use a Semicolon Correctly. The most common use of the semicolon is to join two independent clauses without using a conjunction like and. Do you use a capital letter after a semicolon? The general answer is no. A semicolon should be followed by a capital letter only if the word is a proper noun or an acronym.


In your example, there is really no reason that you could not just move the implementation of the function before main():

#include <iostream>
using namespace std;  // you should avoid this, too

auto func(int i)
{
  if (i == 1)
    return i;
  else
    return func(i-1) + i;
}

int main()
{
    auto ret = func(5);
    return 0;
}

Otherwise you just can't use the auto keyword. In particular, you can't use auto in a recursive function which does not return anything. You have to use void. And that applies to lambda functions. For example:

int main()
{
    auto f = [](int i)
    {
        // ... do something with `i` ...
        if(i > 0)
        {
            f(i - 1);   // <-- same error here
        }
    }

    auto ret(func(5));
    return 0;
}

The call f(i - 1) has a problem. To fix it you have to replace the auto by the actual type:

int main()
{
    typedef std::function<void(int)> func_t;
    func_t f = [](int i)
    {
    ...

If you really want a function which support varying return types you want to use a template anyway, not auto. This is really only to help you with less typing, not so much as a way to allow "any type".

What Are the 14 Punctuation Marks in English Grammar?, The third use of a colon is for emphasis: There was one thing she loved more than any other: her dog. A colon also has non-grammatical uses in time� How to Use Apostrophes. The apostrophe is used for two reasons: to show that certain letters in a contraction are missing, and to indicate ownership. The rules for apostrophes vary with the type of word.


Semicolons, Use a semicolon in place of a period to separate two sentences where the conjunction has been left out. The use of prepositions in English is frequently idiomatic. General guidelines exist, but be prepared to learn individual expressions in which the preposition does not adhere to the guidelines. In the case of the prepositions in and on , here are the most usual uses.


When to Use Commas, Colons, Semicolons, and Dashes, On the other hand, a well-placed semi-colon can add nuance and subtlety to any kind of writing. Keep the following rules in mind when choosing to use a comma (, )� Soon, grammarians tried to establish a set of rules to govern the use of these words. As is so often the case when a grammatical rule is established based on the preference of usage writers, rather than actual usage, there was a great deal of disagreement as to what the rules should actually be.


Use, Use definition is - to put into action or service : avail oneself of : employ. How to use use in a sentence. Synonym Discussion of use. The proper use of the relative pronouns who, that, and which relate the subject of a sentence to its object, hence the name. The question of which of the three words to use in a given context vexes some writers; here’s an explanation of their relative roles.