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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.