I bought a MacBook Pro and I've been using MacOS for the past two days. I've been trying to write this C++ code that outputs the date and time using chrono and ctime libraries.
This code was working just fine on my Windows machine and my CentOS7 server. However, on my MacBook Pro it fails to compile.
This is the error message I get when I try to compile with G++:
main.cpp:19:61: error: no viable conversion from 'time_point<std::__1::chrono::steady_clock,
duration<[...], ratio<[...], 1000000000>>>' to 'const
time_point<std::__1::chrono::system_clock, duration<[...], ratio<[...], 1000000>>>'
std::time_t date = std::chrono::system_clock::to_time_t(now);
^~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from
'std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long,
std::__1::ratio<1, 1000000000> > >' to 'const
std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long,
std::__1::ratio<1, 1000000> > > &' for 1st argument
class _LIBCPP_TEMPLATE_VIS time_point
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: candidate constructor
(the implicit move constructor) not viable: no known conversion from
'std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long,
std::__1::ratio<1, 1000000000> > >' to 'std::__1::chrono::time_point<std::__1::chrono::system_clock,
std::__1::chrono::duration<long long, std::__1::ratio<1, 1000000> > > &&' for 1st argument
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1359:5: note: candidate template
ignored: could not match 'std::__1::chrono::system_clock' against 'std::__1::chrono::steady_clock'
time_point(const time_point<clock, _Duration2>& t,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1566:53: note: passing argument to
parameter '__t' here
static time_t to_time_t (const time_point& __t) _NOEXCEPT;
^
1 error generated.
basavyr#Roberts-MacBook-Pro simpleTest %
Here is the code:
void getTime()
{
auto now = std::chrono::high_resolution_clock::now();
std::time_t date = std::chrono::system_clock::to_time_t(now);
std::cout << std::ctime(&date);
}
int main()
{
getTime();
}
I assume the issue has to do with the fact that MacOS is using CLANG compiler?
Any ideas how can I solve this?
Thank you!
Your code is only likely to work when std::high_resolution_clock is the same type as std::system_clock. There is no guarantee that time points from different clocks are the same or convertible.
For conversion to time_t which only has seconds resolution system_clock will be perfectly adequate:
void getTime()
{
auto now = std::chrono::system_clock::now();
std::time_t date = std::chrono::system_clock::to_time_t(now);
std::cout << std::ctime(&date);
}
int main()
{
getTime();
}
Related
I'm using Clang 14 (on Apple M1), which has full support for C++ 17, and I'm trying to utilize the new to_chars function. Here's my very simple test file:
#include <charconv>
#include <iostream>
int main() {
char a[10];
double pi = 3.141592;
std::to_chars_result res = std::to_chars(a, a+10, pi);
*res.ptr = '\0';
std::cout << a << std::endl;
}
My compile command is clang -std=c++17 test_to_chars.cpp, and the output is below:
test_to_chars.cpp:8:30: error: call to deleted function 'to_chars'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:166:6: note: candidate function has been explicitly deleted
void to_chars(char*, char*, bool, int = 10) = delete;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:450:1: note: candidate template ignored: requirement 'is_integral<double>::value' was not satisfied [with _Tp = double]
to_chars(char* __first, char* __last, _Tp __value)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:458:1: note: candidate function template not viable: requires 4 arguments, but 3 were provided
to_chars(char* __first, char* __last, _Tp __value, int __base)
^
test_to_chars.cpp:8:24: error: no viable conversion from 'void' to 'std::to_chars_result'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'void' to 'const std::to_chars_result &' for 1st argument
struct _LIBCPP_TYPE_VIS to_chars_result
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit move constructor) not viable: cannot convert argument of incomplete type 'void' to 'std::to_chars_result &&' for 1st argument
2 errors generated.
I'm calling to_chars(char*, char*, double) but for some reason it's using an implicit conversion and trying to call to_chars(char*, char*, bool, int = 10) instead, which is a deleted function.
Is there a way for me to tell C++ that I don't want it to convert my double parameter to a bool?
I'm using Clang 14 (on Apple M1), which has full support for C++ 17
This is unfortunately not correct. While the compiler itself has full C++17 support, the stdlib of your clang version (Apple clang 14) does not implement any floating point charconv features.
See the entry "Elementary string conversions" in the cppreference table.
It is important to note that you are not running "clang 14", but "Apple clang 14". Your code snippet compiles just fine on normal clang 14.
I defined a map like this :
std::map<std::string,LexType> lexname_s = { { "PROGRAM" , PROGRAM}}
And a LexType, like this :
typedef enum
{
ENDFILE, ERROR,
PROGRAM, PROCEDURE, TYPE, VAR, IF,
} LexType;
In Visual Studio Code, it always shows error type when I touch it.
//
I add more details for what i said.
the line
std::map<std::string,LexType> lexname_s = { { "PROGRAM" , PROGRAM}}
show error . it seems i can't initialize it in this way.
I compile it in the gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) in a cloud server which is centos7.
AND the error code shows below
from parse.cpp:1:
../utils.h:52:27: error: invalid conversion from ‘int’ to ‘const LexType&’ [-fpermissive]
{"ERROR", ERROR}};
^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/stl_tree.h:61,
from /usr/include/c++/4.8.2/map:60,
from ../globals.h:6,
from parse.h:4,
from parse.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:112:26: error: initializing argument 2 of ‘constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char>; _T2 = LexType]’ [-fpermissive]
_GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)```
EOF(which shows in the map define) is a reserved a macro defined in stdio.h
it's the problem of it.
change the name will be ok.
I tried the following conversions, but all give me a no matching constructor for initialization of 'QStringView' error (comments reflect the constructor I was trying to call):
string someString = "hello world";
string_view strView( someString );
// QStringView(const Char (&)[N] string = N) or
// QStringView(const Char *str, qsizetype len)
QStringView qStrView1(strView.data(), strView.size());
// QStringView(const Char *first, const Char *last)
QStringView qStrView2(strView.data(), strView.data() + strView.size());
// QStringView(const Char *first, const Char *last)
QStringView qStrView3(strView.begin(), strView.end());
QStringView qStrView3a(strView.cbegin(), strView.cend());
// QStringView(const Char *str)
QStringView qStrView4(strView.data());
(I thought the 1st or 2nd conversion might work, and tried the 3rd and 4th just out of disappointment.)
Can someone please point me to the right conversion? Do I miss something?
Or do I need to dublicate all std::strings as QStrings and create QStringViews from these, by reusing the begin/end positions?
( Besides that, I did not now figure out, how to insert a QStringView into a QTableWidgetItem. I would appreciate any help on this problem as well. )
Context
I read out file content as std::string and split it into lines of fields based on separators. In order to do this efficiently, I generated a std::vector of std::string_view to store the fields. Now I want to visualize the strings in a Qt GUI (actually aiming at QTableWidgetItems) and thought QStringView might serve me well for the GUI part. I want to keep BusinessLogic independent of the GUI an therefore avoided any includes of Qt libraries there.
Setting
Qt 5.15.0
CONFIG += c++17
MSVC2019 amd64
Windows 10
Errors
(I removed note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided, note: candidate constructor template not viable: requires single argument 'str', but 2 arguments were provided and alike)
qStrView1:
qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view::size_type' (aka 'unsigned long long')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qStrView2:
qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qStrView3:
qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]
qStrView3a:
qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]
qStrView4:
qstringview.h:103:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'const QStringView' for 1st argument
qstringview.h:103:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'QStringView' for 1st argument
qstringview.h:169:22: note: candidate constructor not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
qstringview.h:196:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qstringview.h:200:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatiblePointer<const char *>::value' was not satisfied [with Pointer = const char *]
qstringview.h:209:5: note: candidate template ignored: requirement 'std::is_same<const char *, QString>::value || std::is_same<const char *, QStringRef>::value' was not satisfied [with String = const char *]
qstringview.h:214:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleStdBasicString<const char *>::value' was not satisfied [with StdBasicString = const char *]
Thanks for pointing me towards std::wstring. It took me a while understand the wchar_t , wstring, wcout, wifstream, ... stuff, but finally I have the following code working:
Note: still requires CONFIG += 17 in .pro file and following includes
#include <QStringView>
#include <string_view>
#include <string>
// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-"; //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv2a(wview.data(), wview.size());
QStringView qstrv2b(wview.data(), wview.data() + wview.size());
where either of the above constructors works fine.
Example using QStringView inside a QLabel
// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);
// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-"; //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv1(wview.data(), wview.size());
// convert to label
QLabel* otherLabel = new QLabel(qstrv1.toLocal8Bit());
mainLayout->addWidget(otherLabel);
Example from file to QLabel
// additinonal standard libararies:
#include <fstream>
#include <sstream>
// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);
// read file with UTF-8 characters (to a std::string)
std::string tempFileContent;
ifstream file("./infile.txt", ios::binary);
file.seekg(0, file.end);
tempFileContent.resize(file.tellg());
file.seekg(0, file.beg);
file.read(&tempFileContent[0], tempFileContent.size());
file.close();
// convert std::string -> std::wstring
std::wstringstream wss;
wss << tempFileContent.c_str();
std::wstring fileContent = wss.str();
// convert std::wstring -> QStringView
// - either work:
QStringView qstrv2(fileContent.data(), fileContent.size());
// convert to label
QLabel* someLabel = new QLabel(qstrv2.toLatin1()); //!< WORKS
mainLayout->addWidget(someLabel);
/* these did not work:
// total garbage
QLabel(QString::fromUtf8(qstrv2.toString().toStdString().c_str()));
// misses just the 'ß'
QLabel* someLabel = new QLabel(qstrv2.toLocal8Bit());
*/
where file content was
a-b-c-ä-ö-ü-ß-0-1-2
interestingly reading a text file with wifstream to a wstring takes much longer than converting a read string to a wstring using wstringstream. (which is still much slower than reading to std::string itself)
The code :
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "this file works";
return 0;
}
Compiler output was ridiculously long. Some 114000 characters long. This is only a portion of it.
Compiler output on compilation :
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:174:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path::string_type&&' {aka 'std::__cxx11::basic_string<wchar_t>&&'}
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: candidate: 'std::filesystem::__cxx11::path::path(std::filesystem::__cxx11::path&&)'
path(path&& __p) noexcept
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path&&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: candidate: 'std::filesystem::__cxx11::path::path(const std::filesystem::__cxx11::path&)'
path(const path& __p) = default;
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'const std::filesystem::__cxx11::path&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate: 'std::filesystem::__cxx11::path::path()'
path() noexcept { }
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate expects 0 arguments, 1 provided
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
So, what you are seeing is an error message, not an output.
If the file bits/stdc++.h is not a file that you created,
then what you need to do is to replace
#include <bits/stdc++.h>
with
#include <iostream>
and that's gonna solve it for you.
I'm trying to compile the following code:
#include <boost/optional.hpp>
void foo(boost::optional<unsigned> x = boost::none);
placed in the file a.cu, with the CUDA compiler, using the following command line:
nvcc a.cu -c --std=c++11 -I/opt/boost/include
but I get a bunch of errors:
a.cu:2:53: error: conversion from ‘const boost::none_t(boost::none_t::init_tag (*)())’ to ‘boost::optional<unsigned int>’ is ambiguous
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:805:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::rval_reference_type) [with T = unsigned int; boost::optional<T>::rval_reference_type = unsigned int&&] <near match>
optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:805:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:800:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::argument_type) [with T = unsigned int; boost::optional<T>::argument_type = const unsigned int&] <near match>
optional ( argument_type val ) : base(val) {}
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:800:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
Why does this happen, and can I circumvent the problem while still actually using boost::optional in (host-side) code compiled with nvcc?
Additional information:
The code compiles fine with g++ 6.3.0 (my distribution's compiler).
This code (or rather, similar code) used to compile and work on an earlier Linux distribution I was using, where the compiler was g++ 5.4.x .
I've tried this with Boost versions 1.65.1 and 1.69.0 .
I've tried this with CUDA versions 9.2.88 and 10.0.130 .
I had the exact same error and was able to get this to work with this modification:
#define BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
#include <boost/optional.hpp>
This is using CUDA 10.0.130, g++ 7.3.0, and Boost 1.68.0.
A partial answer to the second question:
You could consider using Andrzej Krzemieński's neat and self-contained implementation of an optional instead of boost::optional. It works with C++11, which is what you seem to be doing.