我有两个重载operator(),一个采用函数引用,它将任何类型作为参数并返回任何类型.另一个采用函数引用,它接受任何类型作为参数但返回void.在我的课程实例化后,我得到以下错误:
In instantiation of 'A<void, int>':
error: 'void A<T, F>::operator()(void (&)(F)) [with T = void, F = int]' cannot be overloaded
error: with 'void A<T, F>::operator()(T (&)(F)) [with T = void, F = int]'
template <typename T, typename F> struct A {
void operator()(T (&)(F)) {}
void operator()(void (&)(F)) {}
};
void f(int) {}
int main() {
A<void, int> a;
a(f);
}
Run Code Online (Sandbox Code Playgroud)
当第一个模板参数这些错误只发生T是void.我想知道我做错了什么以及为什么我不能以operator()这种方式超载?
我正在寻找从a中删除第一个单词的最佳方法std::string.这就是我所拥有的,但我觉得这是过于令人沮丧的事情.最好和最短的方法是什么?谢谢.
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::string str{"Where is everybody?"};
std::string first;
if (std::stringstream{str} >> first)
{
str.erase(str.begin(), str.begin() + first.size());
}
std::cout << str; // " is everybody?"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的C++继承示例.但我无法理解.当我尝试B从类继承的类的受保护成员时,A它表示A::baz受保护.
#include <iostream>
class A {
public:
int foo;
int bar;
protected:
int baz;
int buzz;
private:
int privfoo;
int privbar;
};
class B : protected A {}; // protected members go to class B, right?
int main() {
B b;
b.baz; // here is the error [A::baz is protected]
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到我做错的事.我试图改变class B : protected A到: public A,但它仍然无法正常工作.
我已经尝试在Google和SO上搜索定义,我找到了使用它们但没有明确定义的示例."块"是指插入符号(^).我在他们描述的cdecl网站上找到了它:
(double (^)(int, long long )) foo
Run Code Online (Sandbox Code Playgroud)
如
cast foo into block(int, long long) returning double
我从来没有见过这个今天之前用过的符号.任何人都可以清楚地描述一个块是什么,并包含一个最小的工作示例吗?谢谢.
我搜索了SO和谷歌,但遗憾的是找不到答案.我正在寻找原型lambda的正确语法.我试过了:
int g = [] () -> int;
Run Code Online (Sandbox Code Playgroud)
但我得到错误.有没有办法对lambda进行原型设计?如果是这样,怎么样?
我有这个代码,其中我试图从传递的参数中获取最大数字.由于某种原因,它不起作用,我不确定为什么.当我输入2个数字时代码有效,但当传递3个或更多时,我得到这些错误:
prog.cpp:在函数'int main()'中:
prog.cpp:31:29:错误:没有匹配函数来调用'max(int,int,int)'
prog.cpp:31:29:注意:候选是:
prog.cpp:24:30:注意:模板constexpr decltype(handle :: helper :: max(max :: args ...))max(Args ...)
prog.cpp:24:30:注意:模板参数推导/替换失败:
prog.cpp:替换'template constexpr decltype(handle :: helper :: max(args ...))max(Args ...)[with Args = {int,int,int }]':
prog.cpp:31:29:从这里需要
prog.cpp:24:30:错误:没有匹配函数来调用'handle :: helper :: max(int&,int&,int&)'
prog.cpp :24:30:注意:候选人是:prog.cpp:11:18:注意:静态T句柄:: helper :: max(T,T)[T = int; Args = {int,int}]
prog.cpp:11:18:注意:候选人需要2个参数,3个提供
prog.cpp:16:18:注意:static T handle :: helper :: max(T,T,Args ...)[与T = int; Args = {int,int}]
prog.cpp:16:18:注意:候选人需要4个参数,3个提供
这是程序:
#include <iostream>
namespace handle
{
template <typename... Args>
struct helper {};
template <typename T, typename... Args>
struct helper<T, Args...> …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个流缓冲区,我在制作overflow()工作时遇到了麻烦.我将缓冲区调整了10个以上的字符并使用重置缓冲区setp.然后我将指针递回到我们离开的地方.由于某种原因,输出不正确:
template <class charT, class traits = std::char_traits<charT>>
class stringbuf : public std::basic_stringbuf<charT, traits>
{
public:
using char_type = charT;
using traits_type = traits;
using int_type = typename traits::int_type;
public:
stringbuf()
: buffer(10, 0)
{
this->setp(&buffer.front(), &buffer.back());
}
int_type overflow(int_type c = traits::eof())
{
if (traits::eq_int_type(c, traits::eof()))
return traits::not_eof(c);
std::ptrdiff_t diff = this->pptr() - this->pbase();
buffer.resize(buffer.size() + 10);
this->setp(&buffer.front(), &buffer.back());
this->pbump(diff);
return traits::not_eof(traits::to_int_type(*this->pptr()));
}
// ...
std::basic_string<charT> str()
{
return buffer;
}
private:
std::basic_string<charT> buffer;
}; …Run Code Online (Sandbox Code Playgroud) 有一个链表示例,其中有人试图附加节点.这是代码的一部分:
void Append(ListElement<T> const* ptr, T const& datum)
{
ListElement<T>* temp = const_cast<ListElement<T>*>(ptr);
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果你要将它丢弃ptr,const那么制作指针有什么意义呢?ptr在函数中的任何地方都没有使用temp,所以将它作为指向非 const 的指针不是更好,所以人们知道对象可能被修改了吗?
是否有理由制作一个参数const,然后将const_cast其放入体内,而不仅仅是使它成为非常数?
为什么这个代码在g ++和clang(libstdc ++)上成功运行,但是给libc ++一个分段错误:
#include <iostream>
int main()
{
std::cout.tie(&std::cout);
std::cout << 123;
}
Run Code Online (Sandbox Code Playgroud)
我认为应该发生的是std::cout.flush()应该在输出实际发生之前调用.为什么会出现分段错误?
为什么这个例子打印:
#include <iostream>
struct X
{
X() = default;
X(X const&) { std::cout << "copy-constructor\n"; }
X(X&&) { std::cout << "move-constructor\n"; }
X& operator=(X)
{
return *this;
}
};
int main()
{
X x, y;
std::cout << "assign from prvalue calls the ";
x = X{};
std::cout << "\nassign from xvalue calls the ";
x = std::move(y);
}
Run Code Online (Sandbox Code Playgroud)
从prvalue调用
分配来自xvalue 的赋值调用move-constructor
这两个X{}和std::move(y)的右值那么为什么只分配给X{}造成拷贝省音?
c++ ×10
c++11 ×5
inheritance ×1
iostream ×1
lambda ×1
objective-c ×1
protected ×1
streambuf ×1
templates ×1