[cfe-dev] Passing argv to loadFromCommandLine

David Blaikie dblaikie at gmail.com
Thu Apr 16 17:23:10 PDT 2015


On Wed, Apr 15, 2015 at 3:50 PM, Daniel Dilts <diltsman at gmail.com> wrote:
> I was able to pass it the following way, but const_cast makes me cringe.
>
> FixedCompilationDatabase::loadFromCommandLine(argc, const_cast<const
> char**>(argv))

I've committed a change in r235150 that should allow your original
code to work without the need for a const_cast.

>
> On Wed, Apr 15, 2015 at 3:32 PM, Daniel Dilts <diltsman at gmail.com> wrote:
>>
>> The problem is that the standard only guarantees that int main(int argc,
>> char *argv[]) will be accepted as the signature of main.  Yes, most
>> compilers will accept anything that it can decay to const char** argv, but
>> that is not guaranteed to work.
>>
>> I often work with compilers that are of questionable quality (the joys of
>> embedded systems).  I don't really trust them to follow the standard
>> correctly, and I certainly don't trust them to do something intuitive like
>> allowing additional qualifiers on argv.
>>
>>
>> On Wed, Apr 15, 2015 at 3:19 PM, Reid Kleckner <rnk at google.com> wrote:
>>>
>>> Sounds like MSVC is worried you might store pointers to const char *'s
>>> into your argv array, and then it would lose const qualifiers. Clang gives a
>>> good diagnostic:
>>>
>>> $ cat t.cpp
>>> void f(const char **);
>>> int main(int argc, char *argv[]) {
>>>   f(argv);
>>> }
>>>
>>> $ clang -c t.cpp
>>> t.cpp:3:3: error: no matching function for call to 'f'
>>>   f(argv);
>>>   ^
>>> t.cpp:1:6: note: candidate function not viable: no known conversion from
>>> 'char **' to 'const char **' for 1st argument
>>> void f(const char **);
>>>      ^
>>>
>>> Maybe use 'const char *argv[]' if you don't intend to modify argv?
>>>
>>> On Wed, Apr 15, 2015 at 2:37 PM, Daniel Dilts <diltsman at gmail.com> wrote:
>>>>
>>>> I am attempting to call FixedCompilationDatabase::loadFromCommandLine.
>>>> Seems pretty straight forward:
>>>>
>>>> FixedCompilationDatabase::loadFromCommandLine(argc, argv);
>>>>
>>>> In VS2013 this gives me an error:
>>>>
>>>> cannot convert argument 2 from 'char *[]' to 'const char **'
>>>>
>>>> I am using the following signature of main (3.6.1/2 C++11 standard):
>>>>
>>>> int main(int argc, char* argv[])
>>>>
>>>> It seems that the compiler should be able to do the conversion
>>>> implicitly, but it isn't.  Is this how it is supposed to work?  If it is,
>>>> should the signature of loadFromCommandLine change so that argv can be
>>>> passed directly?
>>>>
>>>> _______________________________________________
>>>> cfe-dev mailing list
>>>> cfe-dev at cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>>>
>>>
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>



More information about the cfe-dev mailing list