我正在尝试开始使用Alex Stepanov和Paul McJones的"编程元素".在Page5最后一段:他们说,
"本书使用的编程语言无法将值和值类型描述为与对象和对象类型分开."
我的意思是,有人可以给出编程语言的示例,其中可以将值描述为与对象分离的实体.
c++ programming-languages functional-programming generic-programming
假设我想编写一个通用类,它维护一个始终保持在两个值之间的整数.像这样的东西:
template<int Lower, int Upper>
class MyInt {
private:
int m_int;
public:
// Constructors, operators...
};
Run Code Online (Sandbox Code Playgroud)
类不变量就是这样Lower <= m_int <= Upper.当然,MyInt应该具有整数所有的常用操作,比如赋值和算术运算符.如果操作将其置于打破其不变量的状态,MyInt将抛出.但是,在许多情况下,这应该是编译时可检测的.考虑这个示例代码:
int foo = 500;
constexpr int const bar = 500;
MyInt<0,100> a = 15; // OK
MyInt<0,100> b = foo; // Throws at runtime
MyInt<0,100> c = 500; // Compile error?
MyInt<0,100> d = bar; // Compile error?
MyInt<0,100> f = std::integral_constant<int, 500>; // Compile error
Run Code Online (Sandbox Code Playgroud)
因为std::integral_constant,编写适当的构造函数是直截了当的.但是,它可能编译时,检测a距离范围,c以及d有没有关系?指定的值是编译时已知的文字或constexpr常量.
我已经尝试了SFINAE-ing around和whatnot,但我找不到从值语义到模板参数的方法,即使对于这些情况,我声称这些值显然是编译时常量. …
When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitialized_copy: ([uninitialized.copy])
template <class It, class For>
For uninitialized_copy(It first, It last, For dest)
{
using T = typename std::iterator_traits<For>::value_type;
for (; first != last; ++first, (void)++dest)
::new (static_cast<void*>(std::addressof(*dest))) T(*first);
}
Run Code Online (Sandbox Code Playgroud)
This post addresses the following points from the perspective of the standard:
why ::new is …
c++ c++-faq generic-programming placement-new language-lawyer
如何实现Numeric [T]的子类型?我一直在寻找这方面的指南,但没有找到任何.子类型的例子可能是Rational还是Complex?
在此先感谢Troels
Hii,
当我为类分配实现一些程序时,它让我感到震惊的是如何使用C以通用方式实现它.
我知道我们需要使用void指针和函数,但我只是坚持如何做到这一点.请给我一个简单的例子并演示其用法.
就像如何实现比较函数来实现比较排序,或插入到链表中每个节点都有不同类型的元素等...
PS:任何其他问题或文章的链接都是有帮助和欢迎的.
我一直在学习D,特别是对它的通用编程能力感到非常兴奋.代表很精彩,显然他们已经完全取代了成员函数指针,所以当我想实现类似下面的内容时,我陷入了困境:
template <typename T>
void DispatchMethodForAll(std::vector<T*> & container, void (T::* func)(void))
{
for(typename std::vector<T*>::iterator it = container.begin(); it != container.end(); ++it)
(*it)->*func();
}
Run Code Online (Sandbox Code Playgroud)
根据我在D中学到的函数指针和委托,是不是它们都不能这样做,因为函数指针只能为全局函数声明,并且委托必须绑定到一个对象,没有"部分委托" "我能找到.如此处所示,我不能使用委托,因为没有单个对象可以绑定到要调用的方法.
我知道我可以用mixins来做,并且基本上使它成为一个宏.然而,这听起来确实不像D,我认为应该有"正确的方式"
我执行与代理迭代器/引用类型的容器类似,以std::vector<bool>和冲突到了以下问题,我着手进行例举std::vector<bool>(这个问题不是std::vector<bool>!):
#include <vector>
#include <type_traits>
int main() {
using namespace std;
vector<bool> vec = {true, false, true, false};
auto value = vec[2]; // expect: "vector<bool>::value_type"
const auto& reference = vec[2]; // expect: "vector<bool>::const_reference"
static_assert(is_same<decltype(value), vector<bool>::value_type>::value,
"fails: type is vector<bool>::reference!");
static_assert(is_same<decltype(reference),
vector<bool>::const_reference>::value,
"fails: type is const vector<bool>::reference&!");
/// Consequence:
auto other_value = value;
other_value = false;
assert(vec[2] == true && "fails: assignment modified the vector");
Run Code Online (Sandbox Code Playgroud)
有没有办法实现代理类型,以便静态断言传递?
在实现这样的容器时,是否有关于如何处理此问题的指导原则?
也许通过使用转换运算符来auto/ auto&/ auto&& …
我正在编写如下的通用函数.
template<class Iterator, class T>
void foo(Iterator first, Iterator last) {
T a;
cout << a << endl;
// do something with iterators
}
typedef vector<double>::iterator DblPtr;
vector<double> values;
foo< DblPtr, int>();
Run Code Online (Sandbox Code Playgroud)
a如果我将初始化更改为,则此函数会打印出变量的未定义值
///
T a = T()
cout << a << endl;
// do something with iterators
Run Code Online (Sandbox Code Playgroud)
我可以看到初始值是0我所期待的.
如果我调用T a变量初始化为默认值,但如果我调用T a = T()我相信由于优化,应该调用复制构造函数,其值T()仍然是默认值.
我无法理解这两条线背后的区别是什么以及这种情况发生的原因?
c++ templates initialization generic-programming function-templates
我们可以通过无形创建一个文字类型:
import shapeless.syntax.singleton._
var x = 42.narrow
// x: Int(42) = 42
Run Code Online (Sandbox Code Playgroud)
但是,Int(42)如果甚至无法创建类型别名,我如何作为一种类型进行操作
type Answ = Int(42) // won't compile
// or
def doSmth(value: Int(42)) = ... // won't compile
Run Code Online (Sandbox Code Playgroud) 在我的代码中,我有一个名为的接口AbstactDataModel,用作所有数据模型类的起点。实现这一点是为了让我知道无论xyzModel我需要什么类,它都会有一个fromJson(Map<String, dynamic> json)函数,该函数将返回使用给定的 json 和type.
// In abstract_model.dart
abstract class AbstractDataModel {
///
/// returns a String containing the class name
/// For example, the class ColumnModel will return 'column'
///
String get type;
///
/// Will call the [.fromJson] constructor and return a new instance of the
/// object
///
dynamic fromJson(Map<String, dynamic> json);
}
Run Code Online (Sandbox Code Playgroud)
下面是一个扩展了 DataModel 类的示例AbstractDataModel:
// in group_model.dart
class GroupModel extends AbstractDataModel {
int _id; …Run Code Online (Sandbox Code Playgroud)