type-safety

C++ - Safety of accessing element of vector via pointers

你离开我真会死。 提交于 2021-02-07 20:40:28
问题 In a C++ project of mine, I am using a vector to hold a bunch of struct s which hold a number of elements for a simple game (ie: tic-tac-toe, coordinates, x vs o , etc). ie: struct gameMove { int x; int y; int player; int order; }; Every time during a single game, whenever a player makes a move (ie: places an x or o ), the information is stored in the vector via push_back() , to create an "undo" feature, which currently works as expected. In some parts of my undo/redo logic, I am using

Type-safety with ADT and Aux pattern

北城余情 提交于 2021-02-07 09:51:53
问题 I'm designing type-safe code with ADT and Aux-pattern and cannot get rid of some asInstanceOf . Here is the example: sealed trait Source case object FileSystem extends Source case object Network extends Source sealed trait Data { type S <: Source } object Data { type Aux[T <: Source] = Data { type S = T } } case class RegularFile(path: String) extends Data { type S = FileSystem.type } case class Directory(path: String) extends Data { type S = FileSystem.type } case class UnixDevice(path:

Type-safety with ADT and Aux pattern

断了今生、忘了曾经 提交于 2021-02-07 09:51:28
问题 I'm designing type-safe code with ADT and Aux-pattern and cannot get rid of some asInstanceOf . Here is the example: sealed trait Source case object FileSystem extends Source case object Network extends Source sealed trait Data { type S <: Source } object Data { type Aux[T <: Source] = Data { type S = T } } case class RegularFile(path: String) extends Data { type S = FileSystem.type } case class Directory(path: String) extends Data { type S = FileSystem.type } case class UnixDevice(path:

Prohibit generating of apply for case class

血红的双手。 提交于 2021-02-07 02:53:30
问题 I'm writing a type-safe code and want to replace apply() generated for case class es with my own implementation. Here it is: import shapeless._ sealed trait Data case object Remote extends Data case object Local extends Data case class SomeClass(){ type T <: Data } object SomeClass { type Aux[TT] = SomeClass { type T = TT } def apply[TT <: Data](implicit ev: TT =:!= Data): SomeClass.Aux[TT] = new SomeClass() {type T = TT} } val t: SomeClass = SomeClass() // <------------------ still compiles,

Aux-pattern usage compiles without inferring an appropriate type

人盡茶涼 提交于 2021-02-04 19:20:27
问题 Consider the following simple example involving Aux -pattern: sealed trait AdtBase abstract case class Foo(){ type T <: AdtBase } object Foo{ type Aux[TT] = Foo { type T = TT } } abstract case class Bar(){ type T <: AdtBase val foo: Foo.Aux[T] } object Bar { type Aux[TT] = Bar { type T = TT } def apply[TT <: AdtBase](f: Foo.Aux[TT]): Bar = new Bar() { override type T = TT override val foo: Foo.Aux[T] = f } } case class Baz(foo: Foo) def testBaz(baz: Baz) = Bar(baz.foo) //Compiles fine def

Comparing double to an int

﹥>﹥吖頭↗ 提交于 2021-02-04 19:09:45
问题 While reading the book Programming Principles and Practice Using C++ , in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion? 回答1: C++ has a sets of built-in operators defined in [over.built]. The behavior of the equality operator is defined

Comparing double to an int

别来无恙 提交于 2021-02-04 19:09:22
问题 While reading the book Programming Principles and Practice Using C++ , in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion? 回答1: C++ has a sets of built-in operators defined in [over.built]. The behavior of the equality operator is defined

Comparing double to an int

跟風遠走 提交于 2021-02-04 19:09:19
问题 While reading the book Programming Principles and Practice Using C++ , in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion? 回答1: C++ has a sets of built-in operators defined in [over.built]. The behavior of the equality operator is defined

Comparing double to an int

僤鯓⒐⒋嵵緔 提交于 2021-02-04 19:08:56
问题 While reading the book Programming Principles and Practice Using C++ , in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion? 回答1: C++ has a sets of built-in operators defined in [over.built]. The behavior of the equality operator is defined

Comparing double to an int

喜欢而已 提交于 2021-02-04 19:08:18
问题 While reading the book Programming Principles and Practice Using C++ , in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion? 回答1: C++ has a sets of built-in operators defined in [over.built]. The behavior of the equality operator is defined