explicit

Why can't a pointer be automatically converted into a unique_ptr when returning it?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 17:26:23
问题 Let me pose my question through an example. #include <memory> std::unique_ptr<int> get_it() { auto p = new int; return p; } int main() { auto up ( get_it() ); return 0; } This fails to compile with the following error: a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’ return p; ^ Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead? Motivation: I understand it's supposed to be good practice to use smart

FOR XML EXPLICIT

左心房为你撑大大i 提交于 2019-12-06 14:29:35
问题 Say I have this setup: -- tables declare @main table (id int, name varchar(20)) declare @subA table (id int, mid int, name varchar(20)) declare @subA1 table (id int, subAid int, name varchar(20)) declare @subA2 table (id int, subAid int, name varchar(20)) declare @subB table (id int, mid int, name varchar(20)) -- sample data insert @main values (1, 'A') insert @main values (2, 'B') insert @SubA values (1, 1, 'A') insert @SubA values (2, 1, 'B') insert @SubA values (3, 2, 'C') insert @SubA1

Why I am not able to invoke 'explicit a (string x)'?

天涯浪子 提交于 2019-12-06 13:32:11
问题 I am not able to Invoke 'explicit a (string x)' using the object a3, I got two compilation errors such as: [Error] invalid conversion from 'const char*' to 'int' [-fpermissive] [Error] initializing argument 1 of 'a::a(int)' [-fpermissive] My expected out put is 'int int double string'; Could somebody help me to remove these errors? Thanks for your valuble time. #include<iostream> #include<string.h> using namespace std; struct a{ a(int x=0){cout<<" int "; } inline a (double x){cout<<" double "

c++ explicit 修饰符

大憨熊 提交于 2019-12-06 11:49:29
按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class String { String ( const char* p ); // 用C风格的字符串p作为初始化值 //… } String s1 = “hello”; //OK 隐式转换,等价于String s1 = String(“hello”); 但是有的时候可能会不需要这种隐式转换,如下: class String { String ( int n ); //本意是预先分配n个字节给字符串 String ( const char* p ); // 用C风格的字符串p作为初始化值 //… } 下面两种写法比较正常: String s2 ( 10 ); //OK 分配10个字节的空字符串 String s3 = String ( 10 ); //OK 分配10个字节的空字符串 下面两种写法就比较疑惑了: String s4 = 10; //编译通过,也是分配10个字节的空字符串 String s5 = ‘a’; //编译通过,分配int(‘a’)个字节的空字符串 s4 和s5 分别把一个int型和char型,隐式转换成了分配若干字节的空字符串,容易令人误解。 为了避免这种错误的发生,我们可以声明显示的转换,使用 explicit 关键字: class

Does implementing Interface both implicit and explicit make sense?

怎甘沉沦 提交于 2019-12-05 23:05:12
问题 I'm currently studying for my MS 70-515 exam. In one of the practices the author implements an interface both implicit as well as explicit. The explicit implementation just calls the implicit implementation. The explicit implementation is just listed without an explanation. Does it make sense to have both an implicit and an explicit implementation of the interface? I would think the explicit implementation is redundant (in this case). public class PassTextBox : TextBox, IScriptControl {

Compiler replaces explicit cast to my own type with explicit cast to .NET type?

大憨熊 提交于 2019-12-05 15:31:17
问题 I have the following code: public struct Num<T> { private readonly T _Value; public Num(T value) { _Value = value; } static public explicit operator Num<T>(T value) { return new Num<T>(value); } } ... double d = 2.5; Num<byte> b = (Num<byte>)d; This code compiles, and it surprises my. The explicit convert should only accept a byte , not a double . But the double is accepted somehow. When I place a breakpoint inside the convert, I see that value is already a byte with value 2 . By casting from

When do these load DLLs : Implicit Linking VS Explicit Linking

落花浮王杯 提交于 2019-12-05 08:11:53
I thought Implicit linking loads a DLL as soon as the application starts because it is also called "load-time dynamic linking". But I found some strange explanations below from the link here( https://msdn.microsoft.com/en-us/library/253b8k2c(VS.80).aspx ). Implicit Linking Like the rest of a program's code, DLL code is mapped into the address space of the process when the process starts up and it is loaded into memory only when needed. As a result, the PRELOAD and LOADONCALL code attributes used by .def files to control loading in previous versions of Windows no longer have meaning. Explicit

Generic explicit cast failure C#

佐手、 提交于 2019-12-05 05:17:06
I've some issues with the following piece of code. I would like to explicit a string to an object, this is working perfectly fine, however, if this object is part of a generic class, this is failing with the following error exception: "Unable to cast object of type 'System.String' to type 'test.B'". Even though I've overloaded the the method. using System; using System.Collections.Generic; namespace test { class Program { static void Main(string [] args) { // These two cast perfectly fine. B x = (B) "abc"; C y = (C) "def"; A <B> a = new A<B>(); a.b(); A <C> b = new A<C>(); b.b(); } } class A<T

Explicit copy constructor and std::sort

一笑奈何 提交于 2019-12-05 00:38:56
When sorting a container of objects having an explicit copy ctor I get compiler errors (from g++ 4.8.2 and clang++ 3.4, both in -std=c++11 mode) that I don't understand. I've created a simple example to demonstrate the problem class A { public: explicit A(int i): m_i(i) {}; explicit A(const A& other): m_i(other.m_i) {}; int i() const {return m_i;}; private: int m_i; }; bool is_less(const A& a, const A& b) { return a.i() < b.i(); } int main(int, char*[]) { std::vector<A> objects; objects.push_back(A(3)); objects.push_back(A(5)); objects.push_back(A(-1)); std::cout << is_less(objects[1], objects

Why can't a pointer be automatically converted into a unique_ptr when returning it?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:13:52
Let me pose my question through an example. #include <memory> std::unique_ptr<int> get_it() { auto p = new int; return p; } int main() { auto up ( get_it() ); return 0; } This fails to compile with the following error: a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’ return p; ^ Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead? Motivation: I understand it's supposed to be good practice to use smart pointers for ownership to be clear; I'm getting a pointer (which I own) from somewhere, as an int* in