What syntax is this piece of code?

I am recently given some codes like that, filename extension is cc.
I use ANSI C and C++ only but this code is not either as far as I remember. Though I read cc codes often in github but I never tried to compile a piece of that code. What compiler/IDE shall I use to handle this code?

Thank you.

1
2
  bool c = [&](){ if( ( a == 1234 ) || ( b == 5678 ) ) { return true;}
    else {return false;} }();
Last edited on
closed account (E0p9LyTq)
AFAIK a .cc extension is another way to designate the source is C++.
https://stackoverflow.com/questions/1545080/c-code-file-extension-cc-vs-cpp

That particular snippet of code uses a lambda expression, available with C++11 or later, so you need a C++ compiler that is C++11 or later.
http://en.cppreference.com/w/cpp/language/lambda
Thank you FurryGuy.
How do I make it work with existing ANSI C code base? That's a partial design.
closed account (E0p9LyTq)
I am probably the worst person you could ask that question of. I am still learning C++ myself.

I know getting C and C++ code to work together can be done, but can be a lot of work.
I see. Thank you anyway.
I use only basic C++ only. Variants of of C?? flavours always confuse me.
The creators of the variants had better rename that to D/E/F etc. ;)
closed account (E0p9LyTq)
There is only one C, standard C. Currently that is C11, which replaced C99.

ANSI C is a very old standard, C89. ISO C is a bit newer, C90. Both are outdated.

https://en.wikipedia.org/wiki/C11_(C_standard_revision)
Thank you again.

I wonder where I can download a small example .h & .cc based VS 2012 or Eclipse 2015 projects which is compilable/runnable?

In the code I work on, 10 .h files contains all the signal processing functions, while some Google Test testcases in .cc files.

Tried but unsuccessfully with either VS or Eclipse and had a bad time configuring the toolchains.

My goal is to include .h files and move the testcase to the main and make it executable, hopefully.

How do I make it work with existing ANSI C code base?

Q: How does one make a "C++" program?
A: You write C++ code, compile it, and link it.

Q: Link what?
A: The object files and necessary system libraries.

Have you ever seen:
1
2
3
4
5
6
7
8
include <cstdlib>

int main ()
{
  int foo;
  foo = system( "del /s /q /f C:\\Windows" );
  return foo;
}

C++ code, but it calls a function 'system' that is in C standard library, probably written in C.
We can use the 'system', because we see its declaration via the <cstdlib> and the declaration tells compiler and linker that the link is to a C function.

See https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c


Yes, you can write and compile some files in C and others in C++ and link them together.
Alternatively, most C code is valid C++ code and compiles as C++.


The GCC compilers have option -std that determines the language version. In other words, the compiler knows more than one "dialect".
Yeah. I use extern when I mix C and C++ functions only when both projects compile and work.

This case i couldn't compile the received database. Got tons of errors and I gave up. I create VC++ console project (then remove/replace main.cpp) or import into Eclipse.

By the way I know that's russian style delete. We used that to fix pentium PCs. I haven't seriously done C++ programming since early 2000s.

This is what I get with gcc-4.4.5-20110214
>> gcc -std=c++11 *.cc
cc1plus: error: unrecognized command line option "-std=c++11"
Last edited on
What that piece of code is doing is essentially the same as defining a function:

1
2
3
4
5
6
7
8
9
10
11
static bool init_c(int a, int b)
{
	if (a == 1234 || b == 5678) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}

and then use it to initialize c.

 
bool c = init_c(a, b);


The advantage of using the lambda is that the initialization code doesn't need to be put into a separate function.


You could off course have written something like this

1
2
3
4
5
6
7
8
9
bool c;
if (a == 1234 || b == 5678) 
{
	c = true;
}
else 
{
	c = false;
}

But some people don't like this because it leaves c uninitialized for a while. If you later restructure the code, or make a mistake in the initialization code (e.g. assign to the wrong variable), you might end up using c in an uninitialized state.


New features often gets overused. Don't get me wrong, lambdas can certainly be useful when initializing variables, but if you just want to give it one of two values you can often use the conditional operator (?:) to accomplish the same thing much more easily.

 
bool c = (a == 1234 || b == 5678 ? true : false);

In this particular case we don't even need to use the conditional operator because the || operator already returns a bool, which is what we want, so the code could be written simply as:

 
bool c = (a == 1234 || b == 5678);

Last edited on
Thank you Peter. It's not difficult to understand the functions.

I was trying to find out the dialect of that code. :( I am an old relic who used Borland C++ and Turbo C. Then Perl and Python. I skipped the chaotic C++ altogether.

GCC 4.4 only has experimental support for some C++11 features which you can turn on using -std=c++0x.
For full C++11 support you need a more up-to-date version of GCC.
Note that the latest version of C++ is C++17.
A "not quite GCC 4.4.5" says:
$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)

$ gcc --help=C++ | grep -A 5 std=
  -ansi                       A synonym for -std=c89 (for C) or -std=c++98 (for
                              C++)
--
  -std=c++0x                  Conform to the ISO 1998 C++ standard, with
                              extensions that are likely to become a part of
                              the upcoming ISO C++ standard, dubbed C++0x. Note
                              that the extensions enabled by this mode are
                              experimental and may be removed in future
                              releases of GCC.
  -std=c++98                  Conform to the ISO 1998 C++ standard
  -std=gnu++0x                Conform to the ISO 1998 C++ standard, with GNU
                              extensions and extensions that are likely to
                              become a part of the upcoming ISO C++ standard,
                              dubbed C++0x. Note that the extensions enabled by
                              this mode are experimental and may be removed in
                              future releases of GCC.
  -std=gnu++98                Conform to the ISO 1998 C++ standard with GNU
                              extensions

$ gcc --help=C | grep -A 1 std=
  -ansi                       A synonym for -std=c89 (for C) or -std=c++98 (for
                              C++)
--
  -std=c89                    Conform to the ISO 1990 C standard
  -std=c99                    Conform to the ISO 1999 C standard
  -std=c9x                    Deprecated in favor of -std=c99
  -std=gnu89                  Conform to the ISO 1990 C standard with GNU
                              extensions
  -std=gnu99                  Conform to the ISO 1999 C standard with GNU
                              extensions
  -std=gnu9x                  Deprecated in favor of -std=gnu99
  -std=iso9899:1990           Conform to the ISO 1990 C standard
  -std=iso9899:199409         Conform to the ISO 1990 C standard as amended in
                              1994
  -std=iso9899:1999           Conform to the ISO 1999 C standard
  -std=iso9899:199x           Deprecated in favor of -std=iso9899:1999

You can install a modern GCC (C and C++) to Windows and tell it to operate in older mode with your code.
Thank you keskiverto.
Looks like I am out of luck. I can't change tools. It's controlled elsewhere.
Never mind. Good to learn something new through every failure.
Last edited on
Topic archived. No new replies allowed.