Deferred expression in Visual Studio macros - c++

I tried examples from this page in Visual Studio 2017 (Windows Console Application). And this code doesn't work as expected:
#include <iostream>
using namespace std;
#define EMPTY()
#define DEFER(id) id EMPTY()
#define A() 123
int main() {
cout << DEFER(A)() << endl;
return 0;
}
The code outputs 123 on the console, so DEFER(A)() expands to 123. For instance, in CLion (cygwin 2.9.0) the code works according example from the page, DEFER(A)() expands to A() and causes an error.
So, do Visual Studio macros have some significant differences from others and how to make deferred expressions in Visual Studio in this case? Thanks!

Related

Just installed visual studio; 478 of "E0282 the global scope has no..." errors when compiling C++ program

I'm trying to run a program that prints out "hello" just to see if I can get C++ to run on my machine (running Windows 10 Home and Visual Studio 17.0.5). When I compile the program, I get all kinds of errors that seem to point to files that I haven't even included in my program:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "hello";
return 0;
}
screenshot of errors

OpenMP pragma is ignored in Visual Studio 2017

I have problems getting Visual Studio 2017 to aknowledge
openMP pragmas like "#pragma omp parallel". I understand that Visual Studio should at least support OpenMP 2.0, but the behavior of my program indicates that my pragmas are alltogether ignored.
In an empty C++ project, I enable the OpenMP setting found under "MyProject>Properties>C/C++>Languages>OpenMP Support", and write the following main function:
#include <iostream>
#include <omp.h>
int main(int argc, char* argv[])
{
// I have tried both with and without the following line:
// omp_set_num_threads(15);
#pragma omp parallel
{
std::cout << "Hello world \n";
}
system("pause");
return 0;
}
I expect one "Hello world" to be printed from each active thread in the parallel region. Only one line is printed, indicating that I may have missed something.
Any suggestions?

Can't enter c++ code on Visual Studio 2017

I am creating a Windows console application using visual studio 2017, but the console only accepts c code. I am trying to enter:
#include <iostream>
int main()
{
std::cout << "Hello World";
}
It gives some errors like saying "cout" doesn't exist, "std" doesn't contain "cout" and many others. However if I change the code to:
#include "stdafx.h"
int main()
{
printf("Hello World");
}
It works fine. How do I make it accept c++ code?

Visual Studio 2017: ambiguous symbol size_t in linux projects

In Visual Studio 2017 when creating Linux project and inserting using namespace std; in source code like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i = 1;
string s = to_string(i);
cout << i << s << endl;
return 0;
}
VS underlines size_t and says that it is an ambiguous symbol.
If I press F12 (Go to definition) it offers me two definition places:
From stddef.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\5\include\stddef.h):
// ...
namespace std
{
typedef __SIZE_TYPE__ size_t;
// ...
And c++config.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\c++\5\bits\c++config.h):
// ...
#if !(defined (__GNUG__) && defined (size_t))
typedef __SIZE_TYPE__ size_t;
// ...
It happens only with Linux projects in VS, not with Windows projects.
Is there any known solution (except for "do not use using namespace std; :) )?
Upd: Reported this problem to Microsoft: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
Upd2: Microsoft says that they fixed it, and solution will be in next update: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
It looks like there is a difference between Microsoft's and other compilers related to typedef in and out of a namespace.
This source file
namespace foo { typedef int moo; }
typedef int moo;
using namespace foo;
extern moo a;
compiles in g++ and clang++ (no warnings with -Weverything). MSVC rejects it because of an ambiguous symbol.
This is exactly the situation with size_t in gcc headers. It is typedefed both in and out of namespace std. This doesn't seem to cause any problems with
g++.
Why does this compile in g++ and not in msvc? I guess this is because of different interpretation of 7.1.3/3
In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.
Admittedly the interpretation by g++ is rather loose. The first moo is not declared within namespace :: so the rule seemingly doesn't apply. I cannot find anything else that would permit such a thing.
To solve the problem, I would patch the header where size_t is defined in the global namespace, and bring the declaration inside namespace std (conditionally, if __cplusplus is defined). But I have not tested it (no VC2017 here) and cannot guarantee it will work.
I also have no idea why your code is accepted by the actual compiler and only rejected by IntelliSense. I have tested this construction with actual compilers. (update to the last sentence: I have had the code tested with, and rejected by, MSVC. I have conducted the tests before realising that "the actual compiler" above is in fact gcc and not MSVC).

build does not stop in Visual Studio 2010

I try to build a simple piece of code in Visual Studio, but the building process gets entangled in an infinite loop.
The code is simple:
// test.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
using namespace std;
int main(void)
{
cout << "Hi." << endl;
return 0;
}
Any idea what is going on?
Thanks.
spot several things:
not sure what's in 'stdafx.h'
#include < iostream >
std::cout, std::endl (unless using namespace std; somewhere)
Build works fine for me, though.