要添加const到非const对象,这是首选方法?const_cast<T>或static_cast<T>.在最近的一个问题中,有人提到他们更喜欢使用static_cast,但我认为这const_cast会使代码的意图更加清晰.那么使用static_cast变量const 的论据是什么?
下一个例子的汇编:
class A
{
public:
void foo()
{
}
};
class B : private A
{
public:
using A::foo;
};
int main()
{
typedef void (B::*mf)();
mf func = &B::foo;
B b;
(b.*func)();
}
Run Code Online (Sandbox Code Playgroud)
失败并出现以下错误:
main.cpp||In function ‘int main()’:
main.cpp|18|error: ‘A’ is an inaccessible base of ‘B’
main.cpp|18|error: in pointer to member function conversion
Run Code Online (Sandbox Code Playgroud)
我知道A不是B的可访问基础,但我使用的是using关键字.它不应该允许访问函数foo吗?
标准中哪些相关段落阻止上述编译?
c++ member-function-pointers implicit-conversion private-inheritance
我不明白为什么下面的代码打印struct Value而不是int(这意味着转换构造函数转换为Value而不是int).(Visual C++ 2012)
为什么会这样?为什么编译器完全忽略Value(int)构造函数?
#include <iostream>
#include <type_info>
using namespace std;
struct Value { Value(int) { } };
struct Convertible
{
template<class T>
operator T() const
{ throw typeid(T).name(); }
};
int main()
{
try { Value w((Convertible())); }
catch (char const *s) { cerr << s << endl; }
}
Run Code Online (Sandbox Code Playgroud)
更奇怪的是这次(这次只是C++ 11,在GCC 4.7.2上):
#include <iostream>
#include <typeinfo>
using namespace std;
struct Value
{
Value(Value const …Run Code Online (Sandbox Code Playgroud) 我对scala.collection.JavaConversions有一个非常基本的问题.我希望以下代码可以工作,但是从java.util.List [String]到scala List [String]的隐式转换不会发生.为什么?
import collection.JavaConversions._
import java.util
class Test {
def getStrings() : List[String] = {
val results : java.util.List[String] = new java.util.ArrayList[String]()
results
}
}
Run Code Online (Sandbox Code Playgroud)
我从compi得到以下消息
type mismatch;
found : java.util.List[String]
required: scala.collection.immutable.List[String]
results
^
Run Code Online (Sandbox Code Playgroud) 我从另一个问题(稍加修改)中借用了以下代码,以便在我的代码中使用:
internal class PositiveDouble
{
private double _value;
public PositiveDouble(double val)
{
if (val < 0)
throw new ArgumentOutOfRangeException("Value needs to be positive");
_value = val;
}
// This conversion is safe, we can make it implicit
public static implicit operator double(PositiveDouble d)
{
return d._value;
}
// This conversion is not always safe, so we're supposed to make it explicit
public static explicit operator PositiveDouble(double d)
{
return new PositiveDouble(d); // this constructor might throw exception
} …Run Code Online (Sandbox Code Playgroud) 鉴于lambda的类型是可确定的(可强制转换为std::function(如果我错了,请纠正我)),重载的函数应该同时使用两个函子。已定义?([&]() -> Type {})
请注意,对于我当前的解决方案,我需要按引用捕获,这就是为什么代码包含其逻辑的原因。
以下示例描述了该问题:
#include <iostream>
#include <string>
#include <functional>
void do_some(std::function<void(int)> thing)
{
thing(5);
}
void do_some(std::function<bool(int)> thing)
{
if (thing(10))
{
std::cout << "it's true!" << std::endl;
}
}
int main()
{
int local_to_be_modified = 0;
do_some(
[&](int in)
{
local_to_be_modified = in;
std::cout << "This is void-" << std::endl;
}
);
do_some(
[&](int in) -> bool
{
// error: call to 'do_some' is ambiguous
local_to_be_modified += in;
std::cout << "This is …Run Code Online (Sandbox Code Playgroud) 我想将隐式转换添加到由建模工具生成的Java类中.所以我想将它们添加到这些类的伴随对象中,以便编译器自动找到它们.但我无法将它们添加到单独的文件中,因为必须在同一文件中定义随播广告.我能做些什么吗?
当然,我可以在另一个对象中定义所有隐式转换,然后将其带入范围,但这需要额外的导入.还有其他方法吗?
scala implicit-conversion scala-java-interop companion-object
使用VC++ 2010,给出以下内容:
class Base { };
class Derived : public Base { };
template<class T> void foo(T& t); // A
void foo(Base& base); // B
Derived d;
foo(d); // calls A
foo(static_cast<Base&>(d)); // calls B
Run Code Online (Sandbox Code Playgroud)
我想在上面调用"B".我可以用演员来实现这一点Base,但为什么这是必要的?
我希望为所有不是从Base(内置类型等)派生的类型调用模板函数,但我希望从派生类型调用非模板重载Base,而不需要客户端显式转换.我也尝试使重载成为模板的特化,但在这种情况下会发生相同的行为.得到我正在寻找的东西的惯用方法是什么?
c++ templates overloading type-constraints implicit-conversion
这里到底发生了什么?输出现在为"False":
#include <stdio.h>
int main()
{
if (sizeof(int) > any_negative_integer)
printf("True");
else
printf("False");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为:
if (sizeof(int) < any_negative_integer)
Run Code Online (Sandbox Code Playgroud)
输出为"True".
更新:同样的问题已经被问到,我在问之前找不到它.
TL;DR:我正在寻找与以下 C++20 MWE 等效的 C++14:
template<int sz>
struct bits {
int v; // note explicit(expr) below
explicit(sz > 1) operator bool() const { return bool(v); }
};
int main() {
bool c = bits<1>{1}; // Should work
bool d = bits<3>{1}; // Should fail
}
Run Code Online (Sandbox Code Playgroud)
语境:
bits<sz>我们有一个表示长度为位向量的C++ 类sz。过去,对 all 的转换bool是隐式的sz,但事实证明这很容易出错,因此我们改为operator bool()显式转换。
然而,1-bit位向量(在我们的上下文中)几乎完全等同于布尔值,因此当 时最好是operator bool()隐式的sz == 1。
explicit(sz > 1)这可以在C++20中实现,但我们的目标是 C++14。
我尝试重载 …
c++ implicit-conversion template-meta-programming c++14 c++20