User defined integer literal not found - c++

I am trying to create a user defined literal but get an error message when using it.
GCC says
unable to find numeric literal operator ‘operator""_uint’
while clang tells me
error: no matching literal operator for call to 'operator""_uint' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
I reduced the code to following MCVE:
#include <cinttypes>
unsigned int operator"" _uint(char const *, std::size_t) { return 0; }
int main() {
return 1_uint;
}
Which gives the mentioned error as you can see on ideone.

As you can read in detail on cppreference.com there is a multitude of different versions for literal operators.
The one in the OP is used exclusively for string literals and won't be available for integers. Instead the "fallback" version for integers which expects only a const char* without a second std::size_t parameter:
#include <cinttypes>
unsigned int operator"" _uint(char const *) { return 0; }
int main() {
return 1_uint;
}
Which works on ideone.

Related

comparison of integer expressions of different signedness: correct way of using "for" loop when handling string type

below is c++ code. And I am getting these warnings when compile
comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string::size_type’ {aka ‘long unsigned int’} [-Werror=sign-compare]
15 | for(int x=0; x<s.length();x++)
~^~~~~~~~~~~
different signedness means that I am comparing long unsigned int which is 64 bits to int (which should be 32 bit) I did not know.. that string.length returns that type. So my question is how to write these simple instructions in c++ without any error showing when compiled with -Wall -Werror
I also get following error message. I supposed string class is in namespace std so the question is: when do we get this error and what that error tells. I think its saying that my program uses only namespace which is std so its not required to do std:string s to create a variable . this removes the error string s without namespace. is this correct thinking or is there any other meaning too
f.cpp:7:1: error: label ‘std’ defined but not used [-Werror=unused-label]
7 | std:string s="hello";
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s="hello";
char c[s.length()-1];
s[1]='b';
char d='x';
s.push_back(d);
s+='x';
strcpy(c,s.c_str());
for(int x=0; x<s.length();x++)
{
cout<<c[x];
}
}
Because str.length() returns long unsigned int rather than int, casting it as an int should prevent it from giving an warning or error.
Instead of
std::string str = "Test";
for (int i = 0; i < str.length(); i++) {
something(str[i]);
}
Try
std::string str = "Test";
for (int i = 0; i < (int) str.length(); i++) {
something(str[i]);
}

boost bad lexical cast: source type value could not be interpreted as target when converting a string to unsigned long long

the following piece of code that I compiled on wandbox.org is causing the following error. I don't understand why I am getting the error.
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include "boost/lexical_cast.hpp"
typedef unsigned long long Ulonglong ;
int main()
{
Ulonglong result = boost::lexical_cast<unsigned long long>("862.00");
return 0;
}
Start prog.cc: In function 'int main()': prog.cc:11:15: warning:
unused variable 'result' [-Wunused-variable] 11 | Ulonglong
result = boost::lexical_cast("862.00");
| ^~~~~~ terminate called after throwing an instance of 'boost::wrapexcept' what():
bad lexical cast: source type value could not be interpreted as target
Aborted Finish
It seems boost::lexical_cast must perform an exact conversion, with no extended behaviour. You are trying to cast a string representation of a number containing a decimal point (thus containing a fractional part) to an integer, which is not allowed.
You should either first convert to float/double (mind the data loss for very large integers) and then convert to integer, or cut off the decimal part of the string before handing it off to boost::lexical_cast.

passing literal string as const char * parameter causes code analyzer error

I have overloaded function
int put_message(int level, int sys_log_level, const char * inp_message);
int put_message(int level, int sys_log_level, const std::string &inp_message);
and call this function
put_message(0, LOG_ERR, "clock_gettime error");
Code is compiled and works
but Eclipse CDT Code analyzer says
Invalid arguments '
Candidates are:
int put_message(int, int, const char *)
int put_message(int, int, const ? &)
'
How can I fix this the error?
Update:
After modifying LOG_ERR to int(LOG_ERR) error disappears.
I have not add the in the header.
Adding solves the problem.
you are missing #include <string> or something related to string class
You need to cast the string to the correct type, the compiler treats "some string" as char[] so you need to cast it to const char*
Try with
put_message(0, LOG_ERR, (const char*)"clock_gettime error");

Array bound set by a function of a generic

I want to set the array length to be the minimum of a constant and a generic like this:
template <int foo> struct Bar{
void my_func( int const (&my_array)[std::min(5, foo)] ) { /*...*/ }
};
This code compiles with clang++ but not g++ and I need my code to work with both. The error g++ gives is: error: array bound is not an integer constant before ']' token. How I can set the length of this array to be the minimum of foo and 5?
When I use clang++ I run into the problem that I can't get anything to bind to my_array. I want to run something like:
int main() {
static const int var[5] = {0,1,2,3,4};
Bar<5> bar;
bar.my_func(var);
}
But when I try to compile this code in clang++ I get: error: reference to type 'const int [*]' could not bind to an lvalue of type 'const int [5]'.
If I get rid of the std::min() stuff and replace it with foo the code compiles and runs fine.
Notes:
To get this code to compile you'll need to #include <algorithm> or similar to access std::min.
I don't think that this being part of a template should matter but when I try similar things with non-template function such as:
const int const_five = 5;
void new_func( int const (&my_array)[std::min(5,const_five)] ) { /*...*/ }
g++ says: error: variable or field 'new_func' declared void and clang++ says candidate function not viable: no known conversion from 'const int [5]' to 'const int [std::min(5, const_five)]' for 1st argument which both look like similar problems.
For int const (&my_array)[std::min(5, foo)] to compile, you need a version of std::min which is constexpr. It is since C++14.
Check the default value for -std of gcc and clang you use (its version-dependant). Ultimately, compile with -std=c++14.
Provided by StoryTeller, a nice working MCVE.
Keep it simple:
[foo < 5 ? foo : 5]

Fixing Deprecation Warning

When compiling the below code, it gives me a warning, namely
deprecated conversion from string constant to 'char*'.
In what ways is it possible to remove the message (without explicitly suppressing the warning)?
I tried casting with (const char*), but to no avail.
#include <windows.h>
int main() {
typedef int * (*MyDownloadToUrl)(void*, char*, char*, DWORD, void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
MyDownloadToUrl MyDownloadFunction = (MyDownloadToUrl)GetProcAddress(LibHnd,"URLDownloadToFileA");
MyDownloadFunction(0, "http://MyWebsite.com", "Webpage.htm", 0, NULL);
}
You need to const_cast<char*>("my string literal") to get rid of the warning. In C++03 implicit conversion from a string literal (which is a const char*) to char* is deprecated. In C++11 such an implicit conversion is an error.
In this case though, URLDownloadToFile takes arguments of type LPCTSTR, which is defined as either const wchar_t* or const char* depending on the UNICODE prepossessor directive.