为什么甚至编译?
struct UE{
UE(bool a = true) { };
// UE(){}; // if UE took no initial args and called below, gcc will complain.
};
class VA {
protected:
UE ue;
public:
VA();
};
VA::VA()
{
ue = new UE(true); // ???why???
// ue = new UE(); // will complain
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用gcc(GCC)4.6.2.如何使用指针分配结构?
我正在使用枚举,并尝试从此页面重现一些示例.最初的例子按预期工作,但是我得到了一些有趣的结果,代码如下:
#include <iostream>
enum num : char {
zero = '0',
one = '1',
two = '2',
three = '3',
four = '4',
five = '5',
six = '6'
};
int main()
{
const char two = '2';
std::cout << two << std::endl;
std::cout << num::two;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
2
50
我预计两种结果都是相同的,但num::two似乎打印出其他一些值.此值也不会改变(50),所以我假设这不是随机/垃圾值,并且有一些char/int解析正在完成,我不明白?这是ideone链接.
我知道我可以通过这样分配来实现它zero = 0,没有单引号并且它有效.但是,我想知道幕后发生了什么,我怎么能控制通过单引号分配可以打印的非单位数值.
请考虑以下代码:
void foo(bool parameter) {
std::cout << parameter << "\n";
}
int main() {
foo("const char *argument");
}
Run Code Online (Sandbox Code Playgroud)
我希望编译器在传递时发出警告const char*而不是bool作为参数来发挥作用foo.
但GCC暗中转换它.我想-Wall,-Wextra和-Wpedantic,但这些问题的警告.是否有可以捕获这种隐式转换的标志(无效的参数类型)?
忽略这个函数有一个类型参数的事实,bool有些人可能会认为这是一种糟糕的代码风格.我无法重构那部分.
该标准刚刚提到这种隐式转换将会发生:
可以将整数,浮点,无范围枚举,指针和指针到成员类型的prvalue转换为bool类型的prvalue.
我知道这种行为在if (ptr)语句中非常方便,但对于我来说,在传递参数的情况下,它显然是错误的并且是错误的来源.
这可能是一个简单的问题,但为什么 const char* 不需要指向的内存地址?
例子:
const char* a = "Anthony";
Run Code Online (Sandbox Code Playgroud)
并不是:
const char *a = // Address to const char
Run Code Online (Sandbox Code Playgroud)
像其他类型一样吗?
好的.我已经读过这篇文章了,我对它如何适用于我的例子感到困惑(下图).
class Foo
{
public static implicit operator Foo(IFooCompatible fooLike)
{
return fooLike.ToFoo();
}
}
interface IFooCompatible
{
Foo ToFoo();
void FromFoo(Foo foo);
}
class Bar : IFooCompatible
{
public Foo ToFoo()
{
return new Foo();
}
public void FromFoo(Foo foo)
{
}
}
class Program
{
static void Main(string[] args)
{
Foo foo = new Bar();
// should be the same as:
// var foo = (new Bar()).ToFoo();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经彻底阅读了我链接的帖子.我已阅读C#4规范的第10.10.3节.给出的所有示例都涉及泛型和继承,而上述情况则不然.
任何人都可以解释为什么在这个例子的上下文中不允许这样做?
请不要以"因为规范说明"或仅引用规范的形式发布帖子.显然,规范不足以让我理解,否则我不会发布这个问题. …
我无法理解为什么以下复制初始化无法编译:
#include <memory>
struct base{};
struct derived : base{};
struct test
{
test(std::unique_ptr<base>){}
};
int main()
{
auto pd = std::make_unique<derived>();
//test t(std::move(pd)); // this works;
test t = std::move(pd); // this doesn't
}
Run Code Online (Sandbox Code Playgroud)
A unique_ptr<derived>可以移入a unique_ptr<base>,那么为什么第二个语句有效但最后一个没有?执行复制初始化时是否不考虑非显式构造函数?
gcc-8.2.0的错误是:
conversion from 'std::remove_reference<std::unique_ptr<derived, std::default_delete<derived> >&>::type'
{aka 'std::unique_ptr<derived, std::default_delete<derived> >'} to non-scalar type 'test' requested
Run Code Online (Sandbox Code Playgroud)
从clang-7.0.0开始
candidate constructor not viable: no known conversion from 'unique_ptr<derived, default_delete<derived>>'
to 'unique_ptr<base, default_delete<base>>' for 1st argument
Run Code Online (Sandbox Code Playgroud)
现场代码可在此处获得.
给定类型a和类型b,我如何在运行时确定是否存在从a到b的隐式转换?
如果这没有意义,请考虑以下方法:
public PropertyInfo GetCompatibleProperty<T>(object instance, string propertyName)
{
var property = instance.GetType().GetProperty(propertyName);
bool isCompatibleProperty = !property.PropertyType.IsAssignableFrom(typeof(T));
if (!isCompatibleProperty) throw new Exception("OH NOES!!!");
return property;
}
Run Code Online (Sandbox Code Playgroud)
这是我想要工作的调用代码:
// Since string.Length is an int property, and ints are convertible
// to double, this should work, but it doesn't. :-(
var property = GetCompatibleProperty<double>("someStringHere", "Length");
Run Code Online (Sandbox Code Playgroud) 我想检查变量的地址
volatile int clock;
cout << &clock;
Run Code Online (Sandbox Code Playgroud)
但它总是说x在地址1.我做错了什么?
以下代码在Visual C++ 2013中编译良好,但不在GCC或Clang下编译.
哪个是对的?
通过隐式转换返回对象时是否需要可访问的复制构造函数?
class Noncopyable
{
Noncopyable(Noncopyable const &);
public:
Noncopyable(int = 0) { }
};
Noncopyable foo() { return 0; }
int main()
{
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC:
error: 'Noncopyable::Noncopyable(const Noncopyable&)' is private
Noncopyable(Noncopyable const &);
^
error: within this context
Noncopyable foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)
铛:
error: calling a private constructor of class 'Noncopyable'
Noncopyable foo() { return 0; }
^
note: implicitly declared private here
Noncopyable(Noncopyable const &);
^
warning: C++98 requires an …Run Code Online (Sandbox Code Playgroud) 我有两种类型,T和U,我想知道是否从T到U定义了隐式强制转换运算符.
我知道IsAssignableFrom的存在,这不是我正在寻找的,因为它不涉及隐式转换.
一些谷歌搜索引导我到这个解决方案,但在作者自己的话这是丑陋的代码(它试图隐式转换,如果有异常则返回false,否则为true)
似乎测试是否存在具有正确签名的op_Implicit方法将不适用于基本类型.
是否有更简洁的方法来确定隐式转换运算符的存在?
c++ ×7
.net ×2
c# ×2
reflection ×2
c ×1
c++11 ×1
c++17 ×1
c-strings ×1
casting ×1
copy-elision ×1
cout ×1
enums ×1
gcc ×1
gcc-warning ×1
interface ×1
iostream ×1
unique-ptr ×1