我试图找出防止整数 0 隐式转换为 nullptr_t 然后传递给采用指针的构造函数的最佳方法。显式不会这样做,但我可以获得 nullptr_t 来导致不明确的重载错误:
#include <typeinfo.h>
struct A {
explicit A(char*) { }
};
struct B {
B(nullptr_t a) = delete;
B(char*) { }
};
int main(int argc, char* argv[])
{
A a(0); // darnit I compiled...
B b1(0); // good, fails, but with only b/c ambiguous
B b2((char*)0); // good, succeeds
B b3(1); // good, fails, with correct error
}
Run Code Online (Sandbox Code Playgroud)
还有比这更好的方法吗?另外,删除在这里到底要完成什么?
c++ implicit-conversion explicit-conversion c++11 visual-studio-2013
我正在阅读《Effective C++ 第三版》。在第 70 页,作者说道:
与几乎所有智能指针类一样,
tr1::shared_ptr也auto_ptr重载指针解引用运算符(operator->和operator*),这允许隐式转换为底层原始指针(...)
然后,他展示了一个示例(当时shared_ptr是其中的一部分),该示例具有基于名为 的类的隐式转换:tr1Investment
shared_ptr<Investment> pi1();
bool taxable1 = !(pi1->isTaxFree());
^implicit conversion
shared_ptr<Investment> pi2();
bool taxable2 = !((*pi2).isTaxFree());
^implicit conversion
Run Code Online (Sandbox Code Playgroud)
好吧,从那时起我就写了一些测试用例,并且unique_ptr它们是有效的。
我还发现了有关unique_ptr支持数组的信息,并且shared_ptr也将继续(请参阅注释)。但是,在我的测试中,隐式转换似乎不适用于数组周围的智能指针。
示例:我希望这是有效的......
unique_ptr<int[]> test(new int[1]);
(*test)[0] = 5;
Run Code Online (Sandbox Code Playgroud)
但根据我的编译器(Visual C++ 2015 Update 3),事实并非如此。
然后,通过一点研究,我发现一些证据表明根本不支持隐式转换......例如:https: //herbsutter.com/2012/06/21/reader-qa-why-不要隐式转换为现代智能指针。
此时我很怀疑。它是否支持(标准) ?
注意:这本书在这个主题上可能有点过时,因为作者还在第 65 页上说“没有任何类似auto_ptr或tr1::shared_ptr用于动态分配数组的东西,即使在 TR1 中也没有”。
我对shared_ptr感到困惑,我的主要问题是:当我执行以下操作时,c ++是否创建一个新对象(shared_ptr对象)?
void Func1(const shared_ptr<T>& rhs) {}
void Func2(const shared_ptr<const T>& rhs) {}
shared_ptr<T> v1;
Func1(v1);
Func2(v1);
Run Code Online (Sandbox Code Playgroud)
显然,Func1(v1)是通过 ref 传递的。然而,又如何呢Func2(v1)?
编译者会在后面做以下事情吗?
shared_ptr<const T> tmp_v2 = v1;
Func2(tmp_v2);
Run Code Online (Sandbox Code Playgroud)
我关心它,因为Func2可能比Func1.
非常感谢您的帮助!
shared-ptr implicit-conversion c++11 pass-by-const-reference reference-binding
我有一个计算数字类型的平方和的函数,如下所示。
import cats.syntax.functor._
import cats.syntax.applicative._
import cats.{Id, Monad}
import scala.language.higherKinds
object PowerOfMonads {
/**
* Ultimate sum of squares method
*
* @param x First value in context
* @param y Second value in context
* @tparam F Monadic context
* @tparam T Type parameter in the Monad
* @return Sum of squares of first and second values in the Monadic context
*/
def sumOfSquares[F[_]: Monad, A, T >: A](x: F[A], y: F[A])(implicit num: Numeric[T]) : F[T] = {
def …Run Code Online (Sandbox Code Playgroud) 给出以下示例:我想截断字符串以满足某些长度限制,例如与 SQL 类型的兼容性。
type varchar8 = String
implicit def str2Varchar8(str: String): varchar8 = str.take(8)
val a: varchar8 = "abcdefghi"
// wanted: "abcdefgh", actual result:
a: varchar8 = abcdefghi
Run Code Online (Sandbox Code Playgroud)
编译器似乎没有区分这两种类型。
给定类型别名type A = String,我想要实现的是:
String仅当映射到类型别名时才应用断言/转换的能力A。A即直接使用类型别名作为输入时避免进一步的断言/转换验证示例:
type NotNullA = A
def method(a: A) = if(a != null)
_method(a: NotNullA) // explicit typing
else
???
// "a" at runtime is a String but we consider it validated, instead relying on the type system
protected …Run Code Online (Sandbox Code Playgroud) 我使用第三方容器int来存储大小。我还使用 stl 容器size_t来存储大小。
我经常在我的代码中必须在同一个循环中使用两者,例如:
// vec is std::vector
// list is the third party container
assert(vec.size() == list.size()); // warning
for(size_t i = 0; i < vec.size(); i++)
{
vec[i] = list[i]; // warning
}
Run Code Online (Sandbox Code Playgroud)
因此,为了解决这个问题,我必须要么进行函数风格转换,有人告诉我这是变相的 C 风格转换。
// vec is std::vector
// list is the third party container
assert(int(vec.size()) == list.size());
for(size_t i = 0; i < vec.size(); i++)
{
vec[i] = list[int(i)];
}
Run Code Online (Sandbox Code Playgroud)
或者我可以采用每个人都推荐的更丑陋的解决方案。静态铸造。
// vec is std::vector
// list is the third party …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
template <typename T>
struct wrapper {
T t;
operator T() { return t; }
T get() { return t; }
};
int main() {
int a[10];
int* x = a;
wrapper<long unsigned int> y{2};
std::cout << (x + y); // warning
}
Run Code Online (Sandbox Code Playgroud)
当我在 gcc 上编译它(在 7.3.0 和 8.2.0 上测试)时,-Wsign-conversion我收到“警告:从‘long unsigned int’转换为‘long int’可能会改变结果的符号”。如果y有 type long unsigned int,则没有警告。此外,当我明确调用时y.get()也没有警告:
std::cout << (x + y.get()); // this is ok
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种情况呢?使用用户定义的转换时,是否有一些不能使用的指针算术特殊规则?
我将 int32 类型的张量除以 int32 类型的张量,结果是 float64。我找不到关于为什么会发生这种情况的答案,或者 Tensorflow 如何执行此操作背后是否存在隐含规则。我还没有为任何张量明确定义 dtype,但我已经检查了所有张量,并且在除法之后它们都没有 64 位类型。
我尝试过使用不同的除法公式,例如 tf.divide,都给出相同的结果。
我的代码如下所示:
a_cdf = a / tf.size(a)
Run Code Online (Sandbox Code Playgroud)
具有 tf.int32 类型。
我想要得到的是 float32 形式的结果,这样我就可以编写我的函数而无需显式转换。
我有这个编译时序列生成类(取自此处,并对其进行了一些修改):
#include <cstddef>
template <int...>
struct sequence {};
template <int... Ns>
struct generator;
template <std::size_t Count, int... Ns> // XXX (read below)
struct generator<Count, Ns...> {
using type = typename generator<Count - 1, Count - 1, Ns...>::type;
};
template <int... Ns>
struct generator<0, Ns...> {
using type = sequence<Ns...>;
};
template <std::size_t N>
using sequence_t = typename generator<N>::type;
int main() {
sequence_t<5> a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在 Visual Studio 下编译得很好(即使使用/permissive-开关)。但在 GCC 下会抛出错误:
#include <cstddef>
template <int...> …Run Code Online (Sandbox Code Playgroud) c++ template-specialization language-lawyer implicit-conversion variadic-templates
我有一个不透明类型,它是通过在伴生对象范围中FancyDouble使用从 Double 隐式转换的。given Conversion[Double, FancyDouble] = FancyDouble(_)之后,构造val d: FancyDouble = 0.0工作,但是当我尝试进行类似的比较时if (d == 0.0),编译器抱怨我无法比较FanceDoubleand Double(我期望它应该将0.0文字隐式转换为 FancyDouble,就像前面的情况一样。
如何启用与隐式转换的比较?
c++ ×5
c++11 ×4
scala ×3
arrays ×1
casting ×1
gcc ×1
implicit ×1
monads ×1
scala-3 ×1
scala-cats ×1
shared-ptr ×1
standards ×1
static-cast ×1
tensorflow ×1
typechecking ×1