C++ compilation error - C2059 syntax error: '...'

Hello,
I am trying to run this simple program in visual studio 2017 community edition and I am getting compilation error
Error      C2059      syntax error: '...'      

where am I going wrong here? Please help

#include "stdafx.h"
#include<iostream>

using namespace std;

template <typename ...Args>
void Printer(Args&&... args)
{
	(cout << ... << args) << endl;
}

int main()
{	
	Printer(1, 2, 3, "abc");
    return 0;
}

Open in new window

LVL 1
Anjeneya MurthySr. ConsultantAsked:
Who is Participating?
 
ZoppoCommented:
Hm - probably there's something wrong in the project settings.

Please verify these project setting:

  • General->Platform Toolset: Should be "Visual Studio 2017 (v141)"
  • C/C++->Language->C++ language standard: Should be "/std:c++17" or "/std:c++latest")

If this doesn't help I don't really have more ideas - probably it's differs in different versions (I use VS 2017 Professional), but IMO that's hard to believe.

Best regards,

ZOPPO
0
 
Fabrice LambertFabrice LambertCommented:
Hi,

First thing, using the directive "using namespace std" is considered a bad practice.
This directive was introduced when namespace was added to C++ (back in 1990), and it's sole purpose is to ensure compatibility with older code base. So it have no business in modern codes.
Either prefix all your function call by its namespace, or precisely select what you need locally in your functions.
Ex:
    // prefix
int main()
{
    std::cout << "Some text." << std::endl;
}

    // selection
int main()
{
    using std::cout;
    using std::endl;

    cout << "Some text." << endl;
}

Open in new window

Concerning your issue, this can be solved with a recursive function template.
Notice that there are now 2 template parameters, 1 is fixed, the other is variadic:
#include<iostream>

    // non recursive version to end recursivity
void Printer()
{
    std::cout << std::endl;
}

template <typename Arg1, typename ...Args>
void Printer(const Arg1& first,  Args&&... others)
{
    std::cout << first;
    Printer(others...);    // recursive call
}

int main()
{
	Printer(1, 2, 3, "abc");
	return 0;
}

Open in new window

0
 
ZoppoCommented:
Hi yamurthy,

it seems this isn't working with first VS 2017 versions, i.e. take a look at https://developercommunity.visualstudio.com/content/problem/131610/c17-fold-expressions-not-working-in-visual-studio.html

I tested it (unfortunateley I don't remember which version) the issue and had the same error, then I installed the latest update (to version 15.5.4), with this it compiles without errors.

So IMO you should try updating to the latest release.

Hope this helps,

ZOPPO
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Anjeneya MurthySr. ConsultantAuthor Commented:
@Zoppo: I have upgraded my VS 2017 to the same version you mentioned. But I still get the same error! I restarted my machine as well :) but no luck.
When I run the same code in http://coliru.stacked-crooked.com/ it works perfectly fine.


@Fabrice Lambert: The code that you gave works fine. But I am learning on how this fold expressions work and was trying out this sample from internet. I was curious what is causing this issue in my environment. I did see the link earlier that Zoppo had provided. But for me even after the upgrade also did not work! :(
0
 
Anjeneya MurthySr. ConsultantAuthor Commented:
Thanks Zoppo. The settings was not updated
C/C++->Language->C++ language standard: Should be "/std:c++17" or "/std:c++latest")

This fixed my issue. :)
0
 
ZoppoCommented:
Fine. I'm glad I could help ...

Have a nice day,

best regards,

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.