Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Default string argument issue

Deepak_Chandan
Beginner
1,063 Views
Hi,
I have the following function:
void SaveToFile(string &fileName, const Int32 preci, const Int32 width, string &header="") const;
I want to make header the optional argument to SaveToFile, however while the above code works fine with gcc, I am getting the following error while using icpc:
error: a reference of type "std::string &" (not const-qualified) cannot be initialized with a value of type "const char [1]"
I cannot figure out why I am getting this error. Can anyone help me out?
Thanks,
Deepak
0 Kudos
7 Replies
TimP
Honored Contributor III
1,063 Views
Apparently, this check was put in for compatibility with Visual Studio, which would reject your specification of an implicit conversion contrary to standard. gcc would have to invoke g++ (possibly according to file suffix) in order to compile something this close to C++, and (depending on how up to date your installation is) you may have to cut back on syntax checking for g++ to accept this as an extension.
0 Kudos
Deepak_Chandan
Beginner
1,063 Views
Hi TimP,
I am compiling my codes on Mac OS X, not Windows. I realized after your post that my Int32 types might have caused confusion; I chose to typedef int to Int32 for my codes to allow for quickly changing all integer types. I also meant g++ and not gcc, my apologies.
However, TimP, I was still not able to understand your reply to my present problem. Could you please explain a bit more? What I want to do is pass a default string value to an input argument, because the argument will not likely be required every single time.
Thanks,
Deepak
0 Kudos
Judith_W_Intel
Employee
1,063 Views
Is SaveToFile inside a class template? If so I think this might represent your issue.
Looks like our compiler gives an error on the declaration of the default argument, but g++ waits until you actual
try to use it, i.e.:

#include
using namespace std;

typedef int Int32;

template
struct C {
void SaveToFile(string &fileName, const Int32 preci, const Int32 width, string &header="") const; // icpc gives an error on the declaration (since there are not template dependent arguments)
};

int main() {
C c;
string s;
c.SaveToFile(s,0,0); // g++ gives an error on the call here,i.e. on the actualuse of the default argument
return 0;
}

0 Kudos
Deepak_Chandan
Beginner
1,063 Views
Hi Judith,
Yes SaveToFile is inside a templated class. How would you suggest I ameliorate the problem so that I can make headers as optional?
Thanks,
Deepak
0 Kudos
Judith_W_Intel
Employee
1,063 Views

I'm fairlysure that the type for the default argument in SaveToFile shouldbe const string& instead of string& header.
To confirm this, is the header parameteractually changed inside SaveToFile() and does that change need to be reflected back to the caller? In other words is there any reason it needs to be passed by reference? My guess is no.

The first parameter (filename) probably should be changed to const string& too.


Judy
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,063 Views
Try this:

template
struct C {
static const string h = ""; // or possibly without const
void SaveToFile(string &fileName, const Int32 preci, const Int32 width, string &header=h) const;
};

Jim Dempsey
0 Kudos
SergeyKostrov
Valued Contributor II
1,063 Views
I'm fairlysure that the type for the default argument in SaveToFile shouldbe const string& instead of string& header...

MS VC++ failed to compile the Judy's test-case without a 'const' specificator for a default STL string based argument.

An MS VC++ error was as follows:

Error C2440: 'default argument' : cannot convert from 'const char [1]' to 'std::string &'

It looks like this is an STL declaration problem because two more C/C++ compilers ( MinGW & Borland C++ ) also failed to compile the test-case.
0 Kudos
Reply