当我尝试float
用作模板参数时,编译器会为此代码而烦恼,同时int
工作正常.
是因为我不能float
用作模板参数吗?
#include<iostream>
using namespace std;
template <class T, T defaultValue>
class GenericClass
{
private:
T value;
public:
GenericClass()
{
value = defaultValue;
}
T returnVal()
{
return value;
}
};
int main()
{
GenericClass <int, 10> gcInteger;
GenericClass < float, 4.6f> gcFlaot;
cout << "\n sum of integer is "<<gcInteger.returnVal();
cout << "\n sum of float is "<<gcFlaot.returnVal();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
main.cpp: In function `int main()':
main.cpp:25: error: `float' is not a …
Run Code Online (Sandbox Code Playgroud) 我基本上不懂clang的-Wweak-vtables
.这是我到目前为止所观察到的:
案例一:(触发警告)
class A {
public:
virtual ~A(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
案例二:(不触发警告)
class A {
public:
virtual ~A(){}
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
案例三:(不触发警告)
class A {
public:
virtual ~A();
};
A::~A(){}
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
案例四:(触发警告)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual ~B(){}
};
int …
Run Code Online (Sandbox Code Playgroud) 我找到了一个不起眼的日志记录错误,事实上长度为2的初始化列表似乎是一个特例!这怎么可能?
代码是使用Apple LLVM版本5.1(clang-503.0.40)编译的CXXFLAGS=-std=c++11 -stdlib=libc++
.
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> Strings;
void print(string const& s) {
printf(s.c_str());
printf("\n");
}
void print(Strings const& ss, string const& name) {
print("Test " + name);
print("Number of strings: " + to_string(ss.size()));
for (auto& s: ss) {
auto t = "length = " + to_string(s.size()) + ": " + s;
print(t);
}
print("\n");
}
void test() {
Strings a{{"hello"}}; print(a, "a");
Strings b{{"hello", "there"}}; print(b, "b");
Strings c{{"hello", …
Run Code Online (Sandbox Code Playgroud) 介绍
随着C++ 14(又名C++ 1y)标准处于接近最终的状态,程序员必须问自己有关向后兼容性以及与此相关的问题.
这个问题
在该问题的答案中,指出该标准有一个附录,专门用于有关修订之间变化的信息.
如果可以解释前面提到的附录中的这些潜在问题,或许在任何与那里提到的相关的正式文件的帮助下,将会有所帮助.
C++ 11
14.8.2 - 模板参数扣除 -
[temp.deduct]
7替换发生在函数类型和模板参数声明中使用的所有类型和表达式中.表达式不仅包括常量表达式如那些出现在数组边界或无类型模板参数而且一般表达式(即非常量表达式)的内部
sizeof
,decltype
和其它上下文允许非常量表达式.
C++ 14
14.8.2 - 模板参数扣除 -
[temp.deduct]
7替换发生在函数类型和模板参数声明中使用的所有类型和表达式中.表达式不仅包括常量表达式如那些出现在数组边界或无类型模板参数而且一般表达式(即非常量表达式)的内部
sizeof
,decltype
和其它上下文允许非常量表达式.替换以词汇顺序进行,并在遇到导致演绎失败的条件时停止.
添加的句子明确说明了在C++ 14中处理模板参数时的替换顺序.
替换顺序通常不会引起很多关注.我还没有找到一篇关于其重要性的论文.也许这是因为C++ 1y尚未完全标准化,但我认为必须引入这样的改变是有原因的.
问题:
我已经读过python中的所有东西都是一个对象,因此我开始尝试不同的类型并调用__str__
它们 - 起初我感到非常兴奋,但后来我感到困惑.
>>> "hello world".__str__()
'hello world'
>>> [].__str__()
'[]'
>>> 3.14.__str__()
'3.14'
>>> 3..__str__()
'3.0'
>>> 123.__str__()
File "<stdin>", line 1
123.__str__()
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
something.__str__()
除了"一切"之外还要工作int
?123
不是一个对象类型的int
?这不是建议的做法(也不是未定义的行为),而是关于将整数类型的所有字节转换为值的c ++标准实际上保证的内容
(unsigned char)0
.
在下面的代码片段中,if-statement使用的表达式是否保证在c ++ 11中被评估为true?
std::memset (
reinterpret_cast<char*> (&a), // int a;
(unsigned char)0,
sizeof (int)
);
if (a == 0) {
...
}
Run Code Online (Sandbox Code Playgroud)
通过阅读C99和C++ 11标准中的引文(在本文中进一步说明),我们发现C99明确保证所有位设置为的整数类型0
将表示该0
类型中的值.
我在C++ 11标准中找不到这种保证.
5.2.1.2/1多字节字符
所有位为零的字节应被解释为与移位状态无关的空字符.这样的字节不应作为任何其他多字节字符的一部分出现.
6.2.6.2/1整数类型
任何填充位的值都是未指定的.45)符号位为零的有符号整数类型的有效(非陷阱)对象表示是相应无符号类型的有效对象表示,并且应表示相同的值.
对于任何整数类型,所有位为零的对象表示应该是该类型中零值的表示.
2.3/3字符集[lex.charset]
基本执行字符集和基本执行宽字符集应各自包含基本源字符集的所有成员,以及表示alert,backspace和回车符的控制字符,以及空字符(分别为null宽字符),其表示具有全零位.
是否有一种简单或标准的方法来使用多图迭代器迭代多图中的唯一键?
即对于一个看起来像的集合:{1, "a"}, {1, "lemon"}, {2, "peacock"}, {3, "angel"}
一个迭代器,它会在{1, "a"}
然后开始递增,指向{2, "peacock"}
然后再次递增会指向{3, "angel"}
?
我尝试在嵌套名称说明符中使用不完整类型,如下所示:
class A;
int b= A::c; // error: incomplete type ‘A’ used in nested name specifier
class A {
static const int c=5;
};
Run Code Online (Sandbox Code Playgroud)
在N3797工作草案的3.4.3/1中没有提到它:
在应用于表示其类,名称空间或枚举的嵌套名称说明符的:: scope resolution运算符(5.1)之后,可以引用类或名称空间成员或枚举器的名称.
那个行为实现依赖于什么?
我的问题是在给定的数组中找到重复的字符序列.简单地说,识别字符出现的模式.
.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
1: | J | A | M | E | S | O | N | J | A | M | E | S | O | N |
'---'---'---'---'---'---'---'---'---'---'---'---'---'---'
.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
2: | R | O | N | R | O | N | R | O | N | R | O | N | R | O | N |
'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'
.---.---.---.---.---.---.---.---.---.---.---.---.
3: | S | H | A | M | I | …
Run Code Online (Sandbox Code Playgroud)