C++ 11标准(N3242)第26.5.1.1节第1段说:
在本子条款26.5中,实例化模板的效果:
[...]
f)一条命名模板类型参数
UIntType是未定义的,除非相应的模板参数是CV-不合格和是下列之一unsigned short,unsigned int,unsigned long,或unsigned long long.
它定义了26.5.3.1中的线性同余生成器.该类的定义如下所示:
template<class UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine
Run Code Online (Sandbox Code Playgroud)
minstd_rand0 似乎违反了这个限制:
typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
minstd_rand0;
Run Code Online (Sandbox Code Playgroud)
因为它使用uint_fast32_t(其不保证之一unsigned short,unsigned int,unsigned long,或unsigned long long)在minstd_rand0名为模板参数UIntType,它似乎有未定义的效果#include <random>,或至少使用minstd_rand0.此问题也适用于其他预定义的RNG,并且在C++ 14中似乎没有修复.
我的问题是:
编辑:我注意到这个缺陷报告似乎与这个问题有关.
使用此代码:
struct A
{
int i;
const int b;
};
// The union is to verify that A is a type that can be used in a union.
union U
{
A a;
int b;
};
int main()
{
U a = {1, 1};
U b = {2, 1};
}
Run Code Online (Sandbox Code Playgroud)
g ++版本4.8.3抱怨错误:
a.cpp:9:4: error: member ‘A U::a’ with copy assignment operator not allowed in union
A a;
^
a.cpp:9:4: note: unrestricted unions only available with -std=c++11 or -std=gnu++11
Run Code Online (Sandbox Code Playgroud)
但是clang 3.5.0编译这段代码没有错误.哪一个是正确的?这是编译器错误吗? …
我的问题与此相似,但有点具体.我正在编写一个函数来读取使用little endian表示的istream中的32位无符号整数.在C这样的东西会起作用:
#include <stdio.h>
#include <inttypes.h>
uint_least32_t foo(FILE* file)
{
unsigned char buffer[4];
fread(buffer, sizeof(buffer), 1, file);
uint_least32_t ret = buffer[0];
ret |= (uint_least32_t) buffer[1] << 8;
ret |= (uint_least32_t) buffer[2] << 16;
ret |= (uint_least32_t) buffer[3] << 24;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用一个类似的东西,istream我遇到了我认为是未定义的行为
uint_least32_t bar(istream& file)
{
char buffer[4];
file.read(buffer, sizeof(buffer));
// The casts to unsigned char are to prevent sign extension on systems where
// char is signed.
uint_least32_t ret = (unsigned char) buffer[0];
ret …Run Code Online (Sandbox Code Playgroud) 这段代码会导致未定义的行为吗?
header.h
#ifdef __cplusplus
extern "C"
{
#endif
inline int foo(int a)
{
return a * 2;
}
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
def.c
#include "header.h"
extern inline int foo(int a);
Run Code Online (Sandbox Code Playgroud)
use.c
#include "header.h"
int bar(int a)
{
return foo(a + 3);
}
Run Code Online (Sandbox Code Playgroud)
main.cpp
#include <stdio.h>
#include "header.h"
extern "C"
{
int bar(int a);
}
int main(int argc, char** argv)
{
printf("%d\n", foo(argc));
printf("%d\n", bar(argc));
}
Run Code Online (Sandbox Code Playgroud)
这是一个程序的例子,其中一个inline函数必须在C和C++中使用.如果def.c被删除并且foo没有在C中使用它会起作用吗?(这假设C编译器是C99.)
编译时,此代码有效:
gcc -std=c99 -pedantic -Wall -Wextra -c -o …Run Code Online (Sandbox Code Playgroud) 这似乎与包含常量成员的POD结构类似,但有点相反.
#include <iostream>
struct A
{
int a;
};
union U
{
volatile A a;
long b;
};
int main()
{
U u1;
U u2;
u1.a.a = 12;
u2 = u1;
std::cout << u2.a.a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.8.3编译此代码时没有错误,并且运行正常:
$ g++ -std=c++03 a.cpp -o a_gcc
$ ./a_gcc
12
Run Code Online (Sandbox Code Playgroud)
但是clang ++ 3.5.1会产生错误(我手动包装错误消息以防止代码框滚动):
$ clang++ -std=c++03 a.cpp -o a_clang
a.cpp:8:7: error: member function 'operator=' not viable: 'this'
argument has type 'volatile A', but function is not marked …Run Code Online (Sandbox Code Playgroud) Dr.Dobb的文章A Portable"typeof"运营商说
但是您不能使用类模板从表达式中提取类型,就像使用函数模板或重载一样.(如果表达式是具有外部链接的名称,则可以通过使用模板非类型参数来实现带有类模板的typeof,但这不是很有用.)
括号中的粗体句是否正确?如果是这样,如何使用模板非类型参数来查找具有外部链接的表达式的类型?