在C++ 11标准,为什么服用nullptr是不允许的,而一个地址,我不明白其中的道理被允许携带自己的std :: nullptr_t实例的地址.除了nullptr是保留关键字这一事实之外,是否有任何指定的推理作出此决定?
仅仅因为它让我很开心,我试图用以下函数环绕这个限制:
decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept
{
return const_cast<decltype(nullptr)*>(reinterpret_cast<const decltype(nullptr)*>(&nref));
}
Run Code Online (Sandbox Code Playgroud)
我不得不在参数上使用reinterpret_cast,因为没有它我得到了歇斯底里的错误:
error: invalid conversion from 'std::nullptr_t*' to 'std::nullptr_t*' [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
当我通过直接传递nullptr来调用此函数时,每次都会得到一个不同的地址.是否为nullptr动态分配地址及时进行比较等?或者(可能更有可能)编译器可能强制执行底层对象的临时副本?
当然,这些都不是至关重要的信息,我只是觉得有趣的是为什么要实施这一特定的限制(以及随后为什么我会看到我的行为).
sqlite3_column_text返回一个const unsigned char*,如何将其转换为std :: string?我试过std :: string(),但是我收到了一个错误.
码:
temp_doc.uuid = std::string(sqlite3_column_text(this->stmts.read_documents, 0));
Run Code Online (Sandbox Code Playgroud)
错误:
1>.\storage_manager.cpp(109) : error C2440: '<function-style-cast>' : cannot convert from 'const unsigned char *' to 'std::string'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud) 我有一个内部使用Boost版本的库,shared_ptr只公开那些.对于我的应用程序,我想std::shared_ptr尽可能使用.遗憾的是,两种类型之间没有直接转换,因为引用计数的东西是依赖于实现的.
有没有办法让a boost::shared_ptr和a std::shared_ptr共享同一个ref-count-object?或者至少从Boost版本中窃取引用计数,只让stdlib版本处理它?
我正在尝试做一些非常简单的事情,然而,经过一个小时的搜索,我找不到合适的答案,所以我必须错过一些相当明显的东西.
我正在尝试动态创建用于ifstream的文件名.虽然我理解可以使用各种方法,但我已经决定创建一个std :: string,并使用stringname.c_str来转换为const.
但问题是我需要使用混合变量和预定义文本值来创建字符串.我收到编译器错误,所以这必须是语法问题.
伪
std::string var = "sometext" + somevar + "sometext" + somevar;
Run Code Online (Sandbox Code Playgroud)
谢谢!
这可能只是一个我没有看到的简单错误,但我认为我只是做错了什么.不要担心我在我的标题函数中没有使用命名空间std或者似乎是这个人的问题[问题我读类似于我的] [1] [1]:为什么我得到字符串不命名类型错误?
我现在收到4个错误:
C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.h | 8 | error:名称空间'std'中的'string'未命名类型
C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.h | 12 | error:名称空间'std'中的'string'未命名类型
C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.h | 13 | error:命名空间'std'中的'string'没有命名类型
C:\ Documents and Settings\Me\My Documents\C++ Projects\C++\RandomSentence\Nouns.cpp | 9 | error:no'std :: string Nouns :: nounGenerator()'成员函数在类'Nouns'中声明|
|| ===构建完成:4个错误,0个警告=== |
这是我的头文件:
class Nouns
{
public:
Nouns();
std::string noun;
protected:
private:
int rnp; // random noun picker
std::string dog, cat, rat, coat, toilet, lizard, mime, clown, barbie, pig, …Run Code Online (Sandbox Code Playgroud) 使用C++,是否存在与'\t'换行符相同的标准库常量?
理想的情况是:
std::stringstream ss;
ss << std::tab << "text";
Run Code Online (Sandbox Code Playgroud)
如果没有,为什么会这样呢?
(我知道我可以插入一个,'\t'但我想满足我的好奇心).
是否有任何现有的迭代器实现(可能在boost中)实现某种展平迭代器?
例如:
unordered_set<vector<int> > s;
s.insert(vector<int>());
s.insert({1,2,3,4,5});
s.insert({6,7,8});
s.insert({9,10,11,12});
flattening_iterator<unordered_set<vector<int> >::iterator> it( ... ), end( ... );
for(; it != end; ++it)
{
cout << *it << endl;
}
//would print the numbers 1 through 12
Run Code Online (Sandbox Code Playgroud) 如何从STL容器中删除元素,具有指定值或满足某些条件?
对于不同类型的容器,是否有单一的通用或统一的方法?
考虑这个代码:
#include <functional>
#include <typeinfo>
template <typename T>
inline constexpr const void *foo = &typeid(T);
int main()
{
constexpr bool a = std::less<const void*>{}(foo<int>, foo<float>);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用<而不是std::less这里,代码不会编译。这并不奇怪,因为如果指针指向不相关的对象,则关系指针比较的结果是未指定的,而且显然这样的比较不能在编译时完成。
Run Code Online (Sandbox Code Playgroud)#include <functional> #include <typeinfo> template <typename T> inline constexpr const void *foo = &typeid(T); int main() { constexpr bool a = std::less<const void*>{}(foo<int>, foo<float>); }
即使我使用std::less. 编译器错误是一样的。std::less似乎<至少在 libstdc++ 和 libc++ 中实现;我在 GCC、Clang 和 MSVC 上得到了相同的结果。
但是,关于 cppreference 的页面 …
我正在寻找对"返回值"描述的含义的详尽解释std::reduce,根据cppreference.com,它是:
也许在我能够正确地理解"返回值"部分背后的想法之后,我可以更好地确定我应该选择std::reduce的位置std::accumulate.
c++ ×10
std ×10
c++11 ×3
string ×2
boost ×1
c++17 ×1
compile-time ×1
iterator ×1
namespaces ×1
nullptr ×1
shared-ptr ×1
stl ×1
variables ×1