LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2024, 03:47 PM   #1
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Rep: Reputation: 0
function declaration


is any differences for declare function:

Code:
int foo(int) {
   //...
   return (0);
}
with most modern style:

Code:
auto foo(int) -> int {
   //...
   return (0);
}
And the same about main:
Code:
auto main(int argc, char* argv[]) -> int {
   //...
   return (0);
}
Thanks for answer.
 
Old 01-31-2024, 03:57 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
No difference.

The trailing return type style is actually needed for:

A) lambdas

B) template functions where the return type depends on the types of the arguments
 
Old 01-31-2024, 04:00 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Tried the second one, with gcc-13.2, with the newest -std=c2x setting, but it doesn't compile.
Code:
$ gcc -std=c2x Mac1ek.c
Mac1ek.c:1:1: error: ‘auto’ requires a plain identifier, possibly with attributes, as declarator
    1 | auto foo(int) -> int {
 
Old 01-31-2024, 05:03 PM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Doesn't the int argument need a name?
 
Old 01-31-2024, 06:15 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
This compiles fine for me:

Code:
❯ cat auto.cpp
auto main() -> int
{
    return 0;
}
                                                                            
❯ g++ --std=c++23 auto.cpp
 
1 members found this post helpful.
Old 01-31-2024, 07:12 PM   #6
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 998

Rep: Reputation: 470Reputation: 470Reputation: 470Reputation: 470Reputation: 470
NevemTeve - The file needs a .cpp extension.

I wish the C++ standards committee would pick a different name for their new language.
Ed
 
3 members found this post helpful.
Old 01-31-2024, 07:47 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by EdGr View Post
NevemTeve - The file needs a .cpp extension.
And he should be building with g++, not gcc.
 
Old 01-31-2024, 08:05 PM   #8
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
I'll stick to the following form myself unless for some reason I 'need' to use the 'auto' and -> . Keep It Simple (and readable) ...

Code:
int foo(int aVar) 
   {
   //... do something
   return(0);
   }
 
1 members found this post helpful.
Old 01-31-2024, 11:22 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by EdGr View Post
The file needs a .cpp extension.
That's a piece of information which could have been added into the Original Post. (Note: file extension .cc means the same and it saves a byte.)
 
Old 02-01-2024, 08:43 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
If you want to save two bytes, you can use .C (big C)
 
Old 02-01-2024, 09:06 AM   #11
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by dugan View Post
If you want to save two bytes, you can use .C (big C)
.C is no good on a case-insensitive filesystem. I've always used .cc (not that I write much c++).
 
1 members found this post helpful.
Old 02-02-2024, 08:25 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by EdGr View Post
NevemTeve - The file needs a .cpp extension.

I wish the C++ standards committee would pick a different name for their new language.
Ed
hear! hear!
 
Old 02-02-2024, 08:36 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I agree: “it’s different but not better.” No one is ever going to rewrite billions of lines of existing, working, source code.

Of course I am familiar with these idioms from other languages, but that’s what they called them from the start. Is it really worth it to add all this “new syntax” to what you’re still calling, “C++?” Frankly, I don’t think so. I see no “ROI.”

Last edited by sundialsvcs; 02-02-2024 at 08:41 PM.
 
Old 02-06-2024, 07:07 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
An actual example: an older g++ 4.8.2 couldn't compile this piece from harfbuzz-8.3 (src/hb-serialize.hh line 158):
Code:
auto all_links () const -> 
  decltype ((( hb_concat (this->real_links, this->virtual_links) )))
  { return (( hb_concat (this->real_links, this->virtual_links) )); }
but could compile it in this rewritten format:
Code:
    hb_vector_t<link_t> all_links ()
        { return hb_concat (this->real_links, this->virtual_links); }
Obviously, the first one is better, as it is much harder to understand. Mind you, this is the output of the preprocessing, the original is like this:
before:
Code:
auto all_links () const HB_AUTO_RETURN
        (( hb_concat (this->real_links, this->virtual_links) ));
after:
Code:
hb_vector_t<link_t> all_links ()
        { return hb_concat (this->real_links, this->virtual_links); }
 
  


Reply

Tags
c++, c++11



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] QWebView, forward declaration, "error: forward declaration of 'struct QWebView'" Squall90 Programming 1 12-28-2010 05:40 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Unintelligible declaration of signal function Nerox Programming 4 08-11-2004 04:45 PM
Arguments in PHP function declaration Parksy Programming 5 07-04-2004 04:22 PM
Problem with function declaration Linh Programming 3 04-26-2004 04:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration