免责声明:我知道应该避免隐式转换为字符串,并且正确的方法是op<<过载Person.
请考虑以下代码:
#include <string>
#include <ostream>
#include <iostream>
struct NameType {
operator std::string() { return "wobble"; }
};
struct Person {
NameType name;
};
int main() {
std::cout << std::string("bobble");
std::cout << "wibble";
Person p;
std::cout << p.name;
}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In function ‘int main()’:
prog.cpp:18: error: no match for ‘operator<<’ in ‘std::cout << p.Person::name’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, …Run Code Online (Sandbox Code Playgroud) 我有一系列字符串存储在一个数组中,由空格分隔(例如['f','o','o','\ 0','b','a','r','\ 0'...]),我需要把它分成一个std::vector<std::string>或类似的.
我可以写一个10行循环来使用std::find或strlen(事实上我刚才这样做),但我想知道是否有更简单/更优雅的方法来做,例如我忽略的一些STL算法,可以哄骗这样做.
这是一个相当简单的任务,如果有一些聪明的STL技巧可以应用于使其更简单,我也不会感到惊讶.
任何接受者?
我可能失去了一些东西很明显,但我看不到之间有什么区别std::condition_variable和std::condition_variable_any.为什么我们两个都需要?
我试图让我的程序在没有boost使用的情况下工作,但是找不到某些有用模式的替代方案.也就是说,我boost::optional在标准库中找不到-likewise模式.是否有boost::optional(C++ 11或其他地方)的标准替代方案?
#include <iostream>
#include <type_traits>
double f(int i)
{
return i+0.1;
}
struct F
{
public:
double operator ()(int i) { return i+0.1; }
};
int
main(int, char**)
{
std::result_of<F(int)>::type x; // ok
// std::result_of<f(int)>::type x; // error: template argument 1 is invalid
x = 0.1;
std::cerr << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
请解释为什么std::result_of<f(int)>::type x;无效......
cppreference说"(std::result_of)在编译类型中扣除函数调用表达式的返回类型."
有什么问题?
我开始使用c ++ 11,constexpr和模板元编程似乎是一种在微型微控制器上保存稀缺内存的好方法.
有没有办法编写模板来展平constexpr数组列表,我需要的是一种方法:
constexpr std::array<int, 3> a1 = {1,2,3};
constexpr std::array<int, 2> a2 = {4,5};
constexpr auto a3 = make_flattened_array (a1,a2);
Run Code Online (Sandbox Code Playgroud)
我使用gcc 4.8.4(arm-none-eabi),如果需要,可以使用std = c ++ 11或c ++ 1y选项进行编译.
有些情况下,与启动STL一些功能make_前缀一样std::make_pair,std::make_shared,std::make_unique等它为什么一个更好的做法是使用它们,而不是简单地使用构造?
auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );
std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
Run Code Online (Sandbox Code Playgroud)
根据cppreference,该特性std::is_literal_type在C++ 17中已被弃用.问题是为什么以及什么是未来的首选替代品来检查类型是否是文字类型.
C++ 11包括该算法std::partition_point().然而,对于我尝试过的所有情况,它给出了相同的答案std::lower_bound().唯一的区别是方便的T& value参数.
我错过了什么,或者这两个功能或多或少都做了同样的事情?
c++ ×10
std ×10
c++11 ×6
arrays ×1
auto-ptr ×1
boost ×1
c++17 ×1
constexpr ×1
deprecated ×1
partitioning ×1
pointers ×1
result-of ×1
stl ×1
type-traits ×1