win32 API有两个方法StrFormatByteSize和StrFormatByteSizeEx.即使两个方法在语义上做同样的事情,并且Ex计数器部分只提供一个新参数来稍微改变行为,那么它们不能有两个相同函数的重载?
它是c/c ++的限制还是这种尴尬约定的可能原因是什么?
我有一个期望模板化迭代器类型的函数.
它目前取消引用迭代器来检查正在迭代的类型.
template < typename Iterator >
void func( Iterator i )
{
// Inspect the size of the objects being iterated
const size_t type_size = sizeof( *i );
...
}
Run Code Online (Sandbox Code Playgroud)
我最近发现了几种标准的迭代器类型,比如std::insert_iterator定义*i为简单的引用i.
也就是说,sizeof(*i)迭代器本身的大小; 与sizeof(i)或相同sizeof(***i)
有没有通用的方法(支持C++ 03)来确定任何标准迭代器迭代的对象的大小或类型?
基本的C++类型(例如intor)何时会有float未知的初始值?
内存分配的类型如何影响,如果有的话?宣言怎么样?如果它是什么的一员class/ struct/ union?C++ 11与C++ 03或C++ 98有什么不同?
我怀疑,但不知道我的知识是否完整(或者说是正确的)
当我研究模板专业化时,我使用一个非常简单的例子,但我仍然有错误.
#include <iostream>
template <class T>
class chrrr{
public:
T chgchr(T c);
};
template < class T>
T chrrr<T>::chgchr(T c){
return c+1;
}
template <>
class chrrr<char>{
public:
char chgchr(char c);
};
template <>
char chrrr<char>::chgchr(char c){
return c+2;
}
using namespace std;
int main(){
char a='a';
int i=1;
chrrr<int> it;
chrrr<char> ch;
cout<<ch.chgchr(a)<<endl;
cout<<it.chgchr(i)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误说:
line 20: error: template-id ‘chgchr<>’ for ‘char chrrr<char>::chgchr(char)’ does not match any template declaration
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它不匹配?如果我在类定义体中而不是在外侧定义chgchr,它的效果非常好.
在C中,有时我的输出将不会打印到终端,直到我打印换行符\n.例如:
int main()
{
printf("Hello, World");
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Hello World将不会打印到下一个printf (我知道这是通过在gdb中设置断点).有人可以解释为什么会发生这种情况以及如何解决这个问题?
谢谢!
我一直在研究 GCC 的实现std::function(在调试时到达那里,然后偏离了主题)。
据我所知,它在本地存储中存储小型类型,并且任何不适合它的内容都通过 new 运算符分配。然而,构造函数也会检查元函数__location_invariant,它是特征的包装器std::trivially_copyable,如果它不是“位置不变”,它也会在堆上分配它。
我不完全理解它为什么这样做,因为据我了解
::new (storage) T(args)
应该提供与就地构造函数相同的结果
new T(args)
,但就地构造函数不分配任何内存。
例如,如果它使用单个引用计数对象来存储太大而无法放入本地存储的“位置不变”类型,那么对我来说会更有意义,因为这会减少分配和复制的数量。由于非不变对象每次都被分配和复制,因此它们是“位置相关的”,它们不能全部引用相同的存储。
该实现似乎只是堆分配任何不适合和/或不是位置不变的东西(至少我没有看到它这样做?),所以我很困惑为什么它需要检查位置不变性,如果功能上没有明显差异。
我需要能够根据 ISO-8601 标准的子集解析和存储各种日期、时间或两者。
日期的格式如下:
时间的格式如下:
如果同时定义了日期和时间,则还必须定义时区,如下所示:
我尝试使用 Howard Hinnant 的日期库,它与 C++20 标准中的日期库相同。看来我需要使用特定类型来解析不同的格式,这有点烦人。我宁愿能够解析并存储同一类型中的任何格式。
为了说明问题:
sys_time<microseconds> sys_us;
microseconds us;
year_month ym;
year y;
std::istringstream iss;
iss.str("2012-03-04T05:06:07.123456+07:00");
iss >> date::parse("%FT%T%Ez", sys_us); // Only works with this type. (The others can't parse this much info.)
assert(!iss.fail());
iss.str("2012-03-04");
iss >> date::parse("%F", sys_us); // If the date has the full year, month, and day, this works.
assert(!iss.fail());
iss.str("2012-03");
// iss >> date::parse("%Y-%m", sys_us); // This fails; day is missing. …Run Code Online (Sandbox Code Playgroud) 我偶然发现了这段代码.
std::ostringstream str;
/// (some usage)
assert( ! str );
Run Code Online (Sandbox Code Playgroud)
ostringstream在bool上下文中使用时表示什么?
这可能是编译和运行时不正确的用法吗?
#include <iostream>
#include <cstdlib>
using std::cout;
class A
{
public :
A() { cout << "A()" << this << "\n";}
~A() { cout << "~A()" << this << "\n";}
//void func() { }
virtual void debug(int a) { cout << "A::debug";}
private :
int a;
};
class A1 : public A
{
public :
A1() { cout << "A1()"<< this << "\n";}
~A1() { cout << "~A1()"<< this << "\n";}
private :
int a1;
};
class A2 : public A …Run Code Online (Sandbox Code Playgroud) 当我在C++ 03上磨牙时,我学会了几种编写"给我收集东西"功能的方法.但每个都有一些挫折.
template< typename Container >
void make_collection( std::insert_iterator<Container> );
Run Code Online (Sandbox Code Playgroud)
要么:
void make_collection( std::vector<Thing> & );
Run Code Online (Sandbox Code Playgroud)
要么:
std::vector<Thing> make_collection();
Run Code Online (Sandbox Code Playgroud)
使用现代C++标准,是否有一个更加惯用的函数接口来"生成一个填充的容器"?