void hello()
{
cout << "helloworld" << endl;
}
void hello(string s)
{
cout << "hello " << s << endl;
}
void doWork()
{
thread t1(static_cast<void ()>(&hello));
thread t2(static_cast<void (string)>(&hello),"bala");
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)
错误:
thread.cc|19 col 42| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void()'
thread.cc|20 col 48| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void(std::string) {aka void(std::basic_string<char>)}'
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用typedef函数指针或lambda.是不是可以使用static_cast?
"15.6.2初始化基础和成员"(N4713)部分在第11项之后有以下示例:
struct A {
A() = default; // OK
A(int v) : v(v) { } // OK
const int& v = 42; // OK
};
A a1; // error: ill-formed binding of temporary to reference
A a2(1); // OK, unfortunately
Run Code Online (Sandbox Code Playgroud)
关于这个例子最后一行的构造有什么不幸?
我在整个参考文献中搜索了其他可能发生的"不幸"行为,但我找不到.
如果在这种特殊情况下不幸,那么它是否可能被视为非法?
例如,如果我们声明一个文件指针fp并打开一个文件,如下所示:
FILE* fp = fopen("filename","w");
如果文件未打开,fopen则返回NULL到文件指针fp。如果文件打开,文件指针中存储什么fp?
在C ++中,多行注释以开头/*和结尾*/。
但是,如果发生以下情况,则会导致编译错误
/*
int a = 20;
/*
int b = 10;
*/
*/
Run Code Online (Sandbox Code Playgroud)
有什么原因为什么C ++不支持这种“嵌套注释”样式?
我是一名学习 STL C++ 的学生,有一个简单的问题。
我在学习如何在算法库中实现包含函数时想知道。请参阅此代码(此代码来自https://en.cppreference.com/w/cpp/algorithm/includes)
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
for (; first2 != last2; ++first1) {
if (first1 == last1 || *first2 < *first1)
return false;
if ( !(*first1 < *first2) )
++first2;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
本节有问题。
if ( !(*first1 < *first2) )
++first2;
Run Code Online (Sandbox Code Playgroud)
这部分表现为判断是否*first1等于的代码,*first2以确定给定的部分序列是否是有序序列的一部分。我认为。
如果是这样,*first1 == * first2似乎足够了,我想知道为什么!(first1 < first2)。
这种情况有什么特殊原因吗??
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/gregorian/formatters.hpp>
#include <iostream>
using namespace boost::gregorian;
using namespace std;
int main()
{
typedef boost::date_time::month_functor<date> add_month;
date d(2019, Feb, 28);
add_month mf(1);
date d2 = d + mf.get_offset(d);
cout << to_simple_string(d2) << endl;//output:2019-Mar-31
}
Run Code Online (Sandbox Code Playgroud)
输出为2019年3月31日而不是2019年3月28日。这种行为的理论基础是什么?谈论它以类似的添加月份代码在C#和delphi中输出2019年3月28日。
Look at this simple function call:
f(a(), b());
Run Code Online (Sandbox Code Playgroud)
According to the standard, call order of a() and b() is unspecified. C++17 has the additional rule which doesn't allow a() and b() to be interleaved. Before C++17, there was no such rule, as far as I know.
Now, look at this simple code:
int v = 0;
int fn() {
int t = v+1;
v = t;
return 0;
}
void foo(int, int) { }
int main() {
foo(fn(), fn());
} …Run Code Online (Sandbox Code Playgroud) 环境:
这是代码:
#include <numeric>
...
auto g = std::gcd(10, 4);
...
Run Code Online (Sandbox Code Playgroud)
我已经打开-std=c++17了编译命令中的选项:
g++ -m64 -std=c++17 -c -g -w -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
Run Code Online (Sandbox Code Playgroud)
然后我得到了错误:
错误:“ gcd”不是“ std”的成员
从此网页开始,std::gcd自C ++ 17开始引入。
在此网页上,我的g ++版本支持C ++ 17。
但是为什么仍然存在错误?相同的代码可以在Visual Studio 2017中编译而不会出现任何错误。
我只是在阅读 C++20 概念的例子。现在我正在尝试创建一个函数,该函数将打印出给定类型是否是哈希表或不使用与部分专业化混合的概念。但不幸的是它不起作用。
#include <iostream>
#include <string>
template<typename T>
concept Hashtable = requires(T a) {
{ std::hash<T>{}(a) } -> std::size_t;
};
struct Foo {};
template <typename T>
void Bar() {
std::cout << "Type T is not a hashtable" << std::endl;
}
template <Hashtable T>
void Bar<T> {
std::cout << "Type T is a hashtable" << std::endl;
}
int main()
{
Bar<Foo>();
Bar<std::string>();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用编译器版本 GCC HEAD 9.0.1,编译器标志是g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.69.0/gcc-head/include -std=gnu++2a "-fconcepts". 它给了我以下编译器错误:
prog.cc:18:6: error: template-id 'Bar<T>' …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×2
c++17 ×2
algorithm ×1
boost ×1
c ×1
c++-concepts ×1
c++14 ×1
c++20 ×1
constructor ×1
date ×1
file-pointer ×1
g++ ×1
reference ×1
shared-ptr ×1
std ×1
stl ×1