我已经定义了一个充当整数的类型.我想为我的类型定义std :: common_type的特化.但是,这种专门化应该能够将bounded_integer(我的类)的common_type与任何其他bounded_integer或内置整数类型的其他参数组合在一起.我希望以下代码都有效:
std::common_type<bounded_integer<1, 10>>::type
std::common_type<bounded_integer<1, 10>, int>::type
std::common_type<int, long, bounded_integer<1, 10>>::type
std::common_type<int, int, long, short, long long, short, bounded_integer<1, 10>, int, short, short, short, ..., short, bounded_integer<1, 10>>::type
Run Code Online (Sandbox Code Playgroud)
我第一次尝试解决这个问题是使用enable_if.但是,我意识到这不允许我区分common_type的库定义,正如我所拥有的那样
#include <type_traits>
class C {};
template<typename T, typename... Ts>
class contains_c {
public:
static constexpr bool value = contains_c<T>::value or contains_c<Ts...>::value;
};
template<typename T>
class contains_c<T> {
public:
static constexpr bool value = std::is_same<T, C>::value;
};
namespace std {
template<typename... Args, typename std::enable_if<contains_c<Args...>::value>::type>
class common_type<Args...> {
public:
using type = …Run Code Online (Sandbox Code Playgroud) c++ template-specialization template-meta-programming variadic-templates c++11
给出以下代码:
struct A;
struct B {
B() {}
B(A &&) {}
};
struct A {
A() {}
A(B &&) {}
};
Run Code Online (Sandbox Code Playgroud)
然后我可以使用任意数量的大括号来构造Aor B。
// default construct A
auto a = A{};
// default construct B, forward to A
auto b = A{{}};
// default construct A, forward to B, forward to A
auto c = A{{{}}};
// etc.
auto d = A{{{{}}}};
auto e = A{{{{{}}}}};
Run Code Online (Sandbox Code Playgroud)
同样,给定
struct C {
C(std::initializer_list<C>) {}
};
Run Code Online (Sandbox Code Playgroud)
那么我也可以使用任意数量的大括号
// default construct …Run Code Online (Sandbox Code Playgroud) 我正在编写一个类似整数的类,它表示一个位于某个范围内的值.例如,值的值bounded::integer<0, 10>在[0,10]范围内.对于这个课程,我已经定义radix了2.
应该怎样的价值digits是bounded::integer<-100, 5>?
怎么样bounded::integer<16, 19>?
使用g ++ 4.9和clang 3.4进行测试,为什么这段代码无法编译:
namespace {
template<typename T>
constexpr auto f(T && t) noexcept {
return true;
}
template<typename T, typename... Ts>
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
return f(ts...);
}
} // namespace
int main() {
f(true, 0, 5u);
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码确实:
namespace {
template<typename T>
constexpr auto f(T && t) noexcept {
return true;
}
template<typename T>
constexpr auto f_helper(T && t) noexcept(noexcept(f(t))) {
return f(t);
}
template<typename T, typename... Ts>
constexpr auto f_helper(T …Run Code Online (Sandbox Code Playgroud) 我的项目使用SCons来管理构建过程.我想支持多个编译器,所以我决定使用,AddOption这样用户就可以在命令行中指定使用哪个编译器(默认情况下是当前编译器所用的编译器).
AddOption('--compiler', dest = 'compiler', type = 'string', action = 'store', default = DefaultEnvironment()['CXX'], help = 'Name of the compiler to use.')
Run Code Online (Sandbox Code Playgroud)
我希望能够为各种编译器提供内置的编译器设置(包括特定编译器的最大警告级别等).这是我目前首次尝试解决方案的方式:
if is_compiler('g++'):
from build_scripts.gcc.std import cxx_std
from build_scripts.gcc.warnings import warnings, warnings_debug, warnings_optimized
from build_scripts.gcc.optimizations import optimizations, preprocessor_optimizations, linker_optimizations
elif is_compiler('clang++'):
from build_scripts.clang.std import cxx_std
from build_scripts.clang.warnings import warnings, warnings_debug, warnings_optimized
from build_scripts.clang.optimizations import optimizations, preprocessor_optimizations, linker_optimizations
Run Code Online (Sandbox Code Playgroud)
但是,我不确定该is_compiler()功能是什么样的.我的第一个想法是直接比较编译器名称(例如'clang ++')与用户传入的内容.但是,当我尝试使用时,这立即失败了scons --compiler=~/data/llvm-3.1-obj/Release+Asserts/bin/clang++.
所以我觉得我会变得更聪明并且使用这个功能
cxx = GetOption('compiler')
def is_compiler (compiler):
return cxx[-len(compiler):] == compiler
Run Code Online (Sandbox Code Playgroud)
这只会查看编译器字符串的结尾,以便忽略目录.不幸的是,'clang …
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/params http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/min http://en.cppreference。 com/w/cpp/numeric/random/uniform_int_distribution/max
看起来成员函数a()相当于成员函数min(),成员函数b()相当于max()。
#include <iostream>
#include <random>
int main() {
std::uniform_int_distribution<int> dist(5, 10);
std::cout << "a " << dist.a() << '\n';
std::cout << "b " << dist.b() << '\n';
std::cout << "min " << dist.min() << '\n';
std::cout << "max " << dist.max() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
印刷
a 5
b 10
min 5
max 10
Run Code Online (Sandbox Code Playgroud)
当使用gcc的标准库编译时。这些函数真的相同吗?如果是,为什么要定义a()和b()?
以下属性注释有效吗?
\nstruct s {\n [[gnu::pure]] s(): m(1) {}\n int m;\n};\nRun Code Online (Sandbox Code Playgroud)\nhttps://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html关于属性的说明pure:
\n\n调用除了返回值之外对程序状态没有明显影响的函数可能有助于优化,例如公共子表达式消除。使用 pure 属性声明此类函数可以让 GCC 避免在使用相同参数值重复调用函数时发出某些调用。
\npure 属性禁止函数修改可通过检查函数 xe2x80x99s 返回值以外的方式观察的程序状态。但是,使用 pure 属性声明的函数可以安全地读取任何非易失性对象,并以不影响其返回值或程序的可观察状态的方式修改对象的值。
\n例如,
\nint hash(char *)属性((pure));
\n告诉 GCC,如果通过 hash 可观察到的程序状态(包括数组本身的内容)在中间不发生变化,则使用相同字符串对函数 hash 的后续调用可以替换为第一次调用的结果。即使 hash 采用\n非常量指针参数,它也不能修改它指向的数组,\n也不能修改程序其余部分可能依赖其值的任何其他对象。\n但是,调用者可以安全地更改数组的内容\n在连续调用该函数之间(这样做会禁用\n优化)。该限制也适用于 C++ 非静态成员函数中 this 指针引用的成员对象。
\n纯函数的一些常见示例是 strlen 或 memcmp。\n有趣的非纯函数是具有无限循环的函数或\n依赖于易失性内存或其他系统资源的函数,\n这些函数可能在连续调用之间发生变化(例如标准 C feof 函数\在多线程环境中)。
\npure 属性对函数\xe2\x80\x99s 定义施加了与 const 属性相似但更宽松的限制:pure 允许函数读取任何非易失性内存,即使它在函数的连续调用之间\n发生变化。诊断使用 pure 和 const 属性声明同一个函数。\n因为纯函数不能有任何可观察到的副作用,所以这样的函数返回 void 是没有意义的。声明这样的\n功能已被诊断。
\n
它如何与 C++ 构造函数交互?如果您将构造函数视为返回该类型值的函数(以类型命名),则s可以安全地标记 的默认构造函数 …
我一直在我的代码中使用cstdint中的类型(例如uint32_t),但现在它们并不完全符合我的需求,特别是在模板方面.
有没有办法指定一个两倍于模板参数大小的整数类型?当我的模板传递给uint32_t时,我需要它为函数中的一个变量创建一个uint64_t.也许更难的是,当通过uint64_t时,我需要它来创建一个'uint128_t'.我可以使用两个模板参数的数组来完成此操作,但是我无法将该数组传递给其他模板函数.这是一个性能关键的代码部分(我正在进行密码学).
与此相关,是否有一些其他标头我可以包括(按优先顺序:标准,升压,其他)给我128位整数?看起来这个问题回答了这个特殊的部分:最快的128位整数库
有没有办法指定我想使用不大于特定大小的最大可用整数?该最大尺寸也是sizeof(T)的函数.
以下是合法的吗?
class Aggregate {
public:
int a;
int b;
};
class Class {
public:
Class():
m_aggregate{
3,
// Here, m_aggregate.a is fully constructed, but m_aggregate is not
m_aggregate.a + 5
} {
}
Aggregate m_aggregate;
};
Run Code Online (Sandbox Code Playgroud)
在生命周期开始之后,但在整个聚合的构造函数完成之前使用聚合的元素是否合法?
使用gcc 4.8.2进行测试似乎行为正确......
我编写了一个实现memcpy的函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
char *memcpy(char *dest,char *src,int n){
char *ch=dest;
while (n--)
*ch++=*src++;
return dest;
}
int main(){
char *src="georgia";
int n=strlen(src);
char *dest=new char[n];
std::cout<<*memcpy(dest,src,n)<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它只打印一个g.为什么?
我有一组函数,我在这样的标题中声明:
actual_function.hpp
#ifndef ACTUAL_FUNCTION_HPP
#define ACTUAL_FUNCTION_HPP
#include <iostream>
#ifdef CONDITION
#warning Compiling version 1
template<typename T>
T fun (T x) {
std::cout << "Version 1 implementation is called.\n";
return x + x;
}
#else
#warning Compiling version 2
template<typename T>
T fun (T x) {
std::cout << "Version 2 implementation is called.\n";
return 2 * x + 1;
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
我试图在一个测试程序中测试该函数的两个版本.我以为我可以用多个翻译单元来做这个,所以我有这样的文件布局:
main.cpp中:
void test_version_1 ();
void test_version_2 ();
int main () {
test_version_1 ();
test_version_2 ();
return 0;
} …Run Code Online (Sandbox Code Playgroud)