首先,我定义了两个相互继承的类.
class A {
};
class B : public A {
};
Run Code Online (Sandbox Code Playgroud)
然后,我声明一个使用以下函数的函数std::function<void(A*)>:
void useCallback(std::function<void(A*)> myCallback);
最后,我std::function从我想要在回调函数中使用的其他地方收到一个不同的(但理论上兼容的)类型:
std::function<void(B*)> thisIsAGivenFunction;
useCallback(thisIsAGivenFunction);
Run Code Online (Sandbox Code Playgroud)
我的编译器(clang ++)拒绝这个,因为它的类型thisIsAGivenFunction与期望的类型不匹配.但是B继承自己A,接受它是有道理的thisIsAGivenFunction.
应该是吗?如果没有,为什么?如果它应该,那么我做错了什么?
我只是想添加下面程序中定义的地图的值:
std::map<int, int> floor_plan;
const size_t distance = std::accumulate(std::begin(floor_plan), std::end(floor_plan), 0);
std::cout << "Total: " << distance;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误C2893:无法专门化函数模板'unknown-type std :: plus :: operator()(_ Ty1 &&,_ Ty2 &&)const'
我正在学习用boost/asio编写代码.许多代码示例都使用了async_accept和bind的组合.在服务器代码中,我遇到了这样的事情:
class Tcp_server
{
public:
Tcp_server()
{
}
void start_accept(int a)
{
if(a>0)
{
cout<<a<<endl;
handle_accept(a-1);
}
}
void handle_accept(int a)
{
if(a>0)
{
cout<<a<<endl;
start_accept(a-1);
}
}
};
Run Code Online (Sandbox Code Playgroud)
如果我创建一个Tcp_server的实例并调用handle_accept或start accept,它就可以工作.但是,如果我删除Tcp_server类封装,编译器会抱怨"未声明handle_accept".我只是想知道编译器是否自动转发声明在同一个类中定义的所有函数.有谁能解释为什么?
请考虑以下声明:
#include <array>
struct X
{
//std::array<bool,3> arr={false,false,false};
bool brr[3]={false,false,false};
};
Run Code Online (Sandbox Code Playgroud)
按原样,它通常由g ++ 5.2编译.但如果我取消注释std::array,我会收到一个错误:
test.cpp:5:46: error: array must be initialized with a brace-enclosed initializer
std::array<bool,3> arr={false,false,false};
^
test.cpp:5:46: error: too many initializers for ‘std::array<bool, 3u>’
Run Code Online (Sandbox Code Playgroud)
OTOH,这个声明里面没有问题main().此外,以下初始化在内部工作struct X:
std::array<bool,3> arr={{false,false,false}};
Run Code Online (Sandbox Code Playgroud)
为什么我不能在结构定义中使用单括号的简单初始化?
我想从字符串中删除前导零"0000000057".
我确实喜欢这个,但没有得到任何结果:
string AccProcPeriNum = strCustData.substr(pos, 13);
string p48Z03 = AccProcPeriNum.substr(3, 10);
Run Code Online (Sandbox Code Playgroud)
我只想输出57.
用C++的想法?
我有以下代码来测试智能指针作为键std::map,我在Mac和Linux上运行代码,但我观察到不同的输出,是一个错误还是我做错了什么?
#include <iostream>
#include <memory>
#include <string>
#include <map>
using namespace std;
class Dog {
public:
typedef shared_ptr<Dog> sptr;
Dog(const string &name) : name_(name) { }
friend bool operator<(const Dog &lhs, const Dog &rhs) {
cout << "Dog::operator< called" << endl;
return lhs.name_ < rhs.name_;
}
friend bool operator<(const sptr &lhs, const sptr &rhs) {
cout << "Dog::operator< sptr called" << endl;
return lhs->name_ < rhs->name_;
}
private:
string name_;
};
void test_raw_object_as_map_key() {
cout << "raw object …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习c ++,所以我编写了一个使用新的c ++ 11 for循环的短程序,这使编译器给我一个我不理解的错误.这是我的c ++代码:
#include <iostream>
#include <cmath>
using namespace std;
float legge_oraria_moto_accelerato(float a[3]){
return a[2]*a[0] + 0.5*a[1]*a[0]*a[0];
}
int corri(float (f)(float array[3]), float arrays[3][3])
{ for(auto i:arrays) cout << f(i) << '\n';
return 0;
}
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器的(g ++ -std = gnu ++ 11)错误:
mezzo.cpp: In function ‘int corri(float (*)(float*), float (*)[3])’:
mezzo.cpp:9:18: error: ‘begin’ was not declared in this scope
{ for(auto i:arrays) cout << f(i) << '\n';
^
mezzo.cpp:9:18: note: suggested …Run Code Online (Sandbox Code Playgroud) 如果我将移动构造函数(或移动赋值运算符)添加到我的库中,我是否会破坏二进制兼容性?这种添加能否以任何方式破坏用户的代码?
class Foo {
public:
Foo();
Foo(Foo const&);
Foo& operator=(Foo const&);
// new methods:
Foo(Foo&&);
Foo& operator=(Foo&&);
};
Run Code Online (Sandbox Code Playgroud) 我已经使用SFINAE表达式来测试类型是否支持 operator<<
namespace details
{
template<typename T>
struct sfinae_true : std::true_type
{
};
template<typename T>
sfinae_true<decltype (std::declval<std::ostream &> () << std::declval<T const &> ())> test_for_ostream (int);
template<typename T>
std::false_type test_for_ostream (long);
}
template<typename T>
struct supports_ostream : decltype (details::test_for_ostream<T> (0))
{
};
Run Code Online (Sandbox Code Playgroud)
我想测试的是,如果这种类型T可以像这样迭代
for (auto && v : vs) {} // vs is T const &
Run Code Online (Sandbox Code Playgroud)
困境在于这是一个陈述,而不是一个使其与之不兼容的表达 decltype
我正在考虑使用lambdas将语句转换为这样的表达式
auto x = [] () { for (auto && v : vs) {}; return 0; } (); // vs …Run Code Online (Sandbox Code Playgroud) 这是一个代码片段,我将使用它来检查可变参数模板类型是否是唯一的:
template <typename...>
struct is_one_of;
template <typename F>
struct is_one_of<F> {
static constexpr bool value = false;
};
template <typename F, typename S, typename... T>
struct is_one_of<F, S, T...> {
static constexpr bool value =
std::is_same<F, S>::value || is_one_of<F, T...>::value;
};
template <typename...>
struct is_unique;
template <>
struct is_unique<> {
static constexpr bool value = true;
};
template <typename F, typename... T>
struct is_unique<F, T...> {
static constexpr bool value =
is_unique<T...>::value && !is_one_of<F, T...>::value;
};
int main() {
constexpr …Run Code Online (Sandbox Code Playgroud) c++ template-meta-programming variadic-templates c++14 c++17
c++ ×10
c++11 ×7
accumulate ×1
arrays ×1
c++14 ×1
c++17 ×1
dictionary ×1
for-loop ×1
function ×1
g++ ×1
map ×1
move ×1
sfinae ×1
shared-ptr ×1
std-function ×1
string ×1
templates ×1