在开发应用程序时,我遇到了以下问题.我想std::list<string>在给定的函数指针为null时返回空,否则返回该函数的结果.这是我的代码的简化版本:
typedef std::list<std::string> (*ParamGenerator)();
std::list<std::string> foo() {
/* ... */
ParamGenerator generator = ...;
if(generator)
return generator();
else
return {};
}
Run Code Online (Sandbox Code Playgroud)
但是,我通常喜欢?:在这些情况下使用ternary()运算符,所以我尝试这样使用它(像往常一样):
return generator ? generator() : {};
Run Code Online (Sandbox Code Playgroud)
但得到了这个错误:
somefile.cpp:143:46: error: expected primary-expression before ‘{’ token
somefile.cpp:143:46: error: expected ‘;’ before ‘{’ token
Run Code Online (Sandbox Code Playgroud)
这是否意味着我不能使用三元运算符来返回使用其构造函数创建的对象initializer_list?这有什么特别的原因吗?
我在检测为什么这不是编译时遇到了麻烦.我有一些std::function基于某些参数返回的lambda函数.
我已经将我的问题缩小到这个片段(它不使用lambdas,但完全重现了我的错误):
#include <functional>
#include <iostream>
struct foo {
template<class T>
void bar(T data) {
std::cout << data << "\n";
}
};
void some_fun(const std::function<void(int)> &f) {
f(12);
}
int main() {
foo x;
auto f = std::bind(&foo::bar<int>, x, std::placeholders::_1);
auto w = std::bind(some_fun, f);
w();
}
Run Code Online (Sandbox Code Playgroud)
调用w()产生一个可爱的gcc错误输出,其中我无法弄清楚出了什么问题.这是gcc 4.6.1回应的错误:
g++ -std=c++0x test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:20:7: error: no match for call to ‘(std::_Bind<void (*(std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>))(const std::function<void(int)>&)>) ()’
/usr/include/c++/4.6/functional:1130:11: note: candidates are: …Run Code Online (Sandbox Code Playgroud) 看看下面的代码:
#include <utility>
#include <map>
// non-copyable but movable
struct non_copyable {
non_copyable() = default;
non_copyable(non_copyable&&) = default;
non_copyable& operator=(non_copyable&&) = default;
// you shall not copy
non_copyable(const non_copyable&) = delete;
non_copyable& operator=(const non_copyable&) = delete;
};
int main() {
std::map<int, non_copyable> map;
//map.insert({ 1, non_copyable() }); < FAILS
map.insert(std::make_pair(1, non_copyable()));
// ^ same and works
}
Run Code Online (Sandbox Code Playgroud)
在取消注释g ++ 4.7上的标记行时,编译此代码段失败.产生的错误表明non_copyable无法复制,但我预计它会被移动.
为什么插入std::pair使用统一初始化的构造失败但没有使用构造std::make_pair?两者都不应该产生可以成功移入地图的右值吗?
我读这文章今天上午从D.卡莱弗关于新的C++ 11功能"拖欠或删除功能",并且无法理解有关性能,即部分:
特殊成员函数的手动定义(即使它是微不足道的)通常比隐式定义的函数效率低.
通过谷歌搜索找到答案,我找到了同一作者的另一篇文章:
合成的构造函数和复制构造函数使实现能够创建比用户编写的代码更高效的代码,因为它可以应用其他方式并不总是可行的优化.
没有解释,但我不时阅读类似的说法.
但是如何写作:
class C { C() = default; };
Run Code Online (Sandbox Code Playgroud)
可以更有效率
class C { C(){} };
Run Code Online (Sandbox Code Playgroud)
?我虽然编译器足够聪明,可以检测到这种情况并对其进行优化.换句话说,编译器在看到=default而不是{}(void body function)时如何更容易优化?
编辑:编辑问题是为了添加"c ++ 11"标签,但这个问题仍然存在于c ++ 03上下文中:只是替换class C {C()=default;};为class C {};,所以不是真正的c ++ 11特定问题.
我正在使用a std::tuple并定义了一个类枚举,以某种方式"命名"每个元组的字段,忘记了它们的实际索引.
所以不要这样做:
std::tuple<A,B> tup;
/* ... */
std::get<0>(tup) = bleh; // was it 0, or 1?
Run Code Online (Sandbox Code Playgroud)
我这样做了:
enum class Something {
MY_INDEX_NAME = 0,
OTHER_INDEX_NAME
};
std::tuple<A,B> tup;
/* ... */
std::get<Something::MY_INDEX_NAME> = 0; // I don't mind the actual index...
Run Code Online (Sandbox Code Playgroud)
问题是,由于使用gcc 4.5.2编译,我现在安装了4.6.1版本,我的项目无法编译.此代码段会重现错误:
#include <tuple>
#include <iostream>
enum class Bad {
BAD = 0
};
enum Good {
GOOD = 0
};
int main() {
std::tuple<int, int> tup(1, 10);
std::cout << std::get<0>(tup) << std::endl;
std::cout << std::get<GOOD>(tup) << …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,pC == pA:
class A
{
};
class B : public A
{
public:
int i;
};
class C : public B
{
public:
char c;
};
int main()
{
C* pC = new C;
A* pA = (A*)pC;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我向B添加一个纯虚函数并在C中实现它时,pA!= pC:
class A
{
};
class B : public A
{
public:
int i;
virtual void Func() = 0;
};
class C : public B
{
public:
char c;
void Func() {}
};
int main()
{
C* …Run Code Online (Sandbox Code Playgroud) 如果我设法用C++构建对象
Object o;
Run Code Online (Sandbox Code Playgroud)
代替
Object *o = new Object();
Run Code Online (Sandbox Code Playgroud)
在任何情况下,我是否需要打电话delete或自动管理所有内存?
我可以使用显式的init-list ctor来确保像{a}这样的表达式不会导致意外的隐式转换吗?而另一个想法:应该我担心呢?写作{a}不太可能是简单的错误a,但另一方面,我们通过隐式转换构建对象的代码仍然可能不清楚.
class Foo
{
explicit Foo (std::initializer_list<Bar> ilist) { /*...*/}
};
Run Code Online (Sandbox Code Playgroud) 今天我尝试了一下我前段时间开发的项目.当遇到编译错误时我感到很惊讶,因为我使用g ++成功编译了我的项目.
这个简短的片段再现了遇到错误的行:
int main() {
__attribute__((aligned(16)) char arr[5];
}
Run Code Online (Sandbox Code Playgroud)
哪会产生这个错误:
test.cpp:2:32: error: expected ')'
__attribute__((aligned(16)) char arr[5];
^
)
Run Code Online (Sandbox Code Playgroud)
如您所见,有一个不平衡的括号.有三个'(',和'')'.这显然看起来应该实际产生编译错误.
这是此关键字的有效用法吗?我似乎无法在文档中找到任何表明它的内容.
我正在使用g ++ 4.5.2和clang 2.8.
请注意,使用gcc而不是g ++时会检测到此错误.
碰到这个不编译的代码:
#include <boost/move/utility.hpp>
#include <utility>
#include <deque>
#include <map>
#include <vector>
#include <boost/date_time/posix_time/posix_time_types.hpp>
using namespace std;
int main() {
typedef std::pair<int, std::deque<int>> FirstPair;
typedef std::vector<FirstPair> VectorFirstPair;
typedef std::pair<boost::posix_time::time_duration, VectorFirstPair> SecondPair;
typedef std::map<boost::posix_time::time_duration, SecondPair> Map;
Map mapInstance;
SecondPair newElement = make_pair(boost::posix_time::not_a_date_time, VectorFirstPair());
mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
}
Run Code Online (Sandbox Code Playgroud)
这在使用boost 1.55(不是在boost 1.54上)的gcc 4.8.2上失败,并出现以下错误(此处为 ideone ):
test.cpp: In function ‘int main()’:
test.cpp:17:81: error: call of overloaded ‘move(SecondPair&)’ is ambiguous
mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
^
test.cpp:17:81: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/utility:70,
from /usr/include/boost/config/no_tr1/utility.hpp:21, …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×6
constructor ×2
bind ×1
boost ×1
c ×1
casting ×1
enum-class ×1
function ×1
gcc ×1
get ×1
movable ×1
noncopyable ×1
performance ×1
tuples ×1
virtual ×1