implicit-conversion

How to write Implicit Conversion from an Interface to another type?

与世无争的帅哥 提交于 2021-02-19 02:36:05
问题 I am trying to do something like below: public class SomeWrapper : ISomeWrapper{ public static implicit operator ActualRec(ISomeWrapper someWrapper) { return ((SomeWrapper)someWrapper).SomeInfo; } } But this code fails, saying: "Either parameter or return type must be of type SomeWrapper". I Understand the problem that compiling is stating. But I need this type conversionb because throughout my application I am using ISomeWrapper as the variable, storing SomeWrapper instance. (Also,

Preventing implicit conversion operator only for binary operators

走远了吗. 提交于 2021-02-19 02:22:09
问题 I'm having an issue that I've boiled down to the following, where an == operator usage compiles even though it's supposed to fail (C++17, tested on GCC 5.x, 8.x, and 9.x): template <int N> struct thing { operator const char * () const { return nullptr; } bool operator == (const thing<N> &) const { return false; } }; int main () { thing<0> a; thing<1> b; a == b; // i don't want this to compile, but it does } The reason it is compiling is because the compiler is choosing to do this: (const char

Deleting all lvalue conversion operators

馋奶兔 提交于 2021-02-18 12:55:08
问题 I want to avoid all lvalue conversions from a type to another type: struct A {}; struct T { A a; operator A() { return a; } //operator A&() = delete; how to delete lvalue conversions to A? }; void bar(A) {} void foo(const A&) {} void foo2(A&) {} int main() { T t; bar(t); // fine foo(t); // should never convert to ref-to-const A foo2(t); // should never convert to ref-to A return 0; } Is this possible? How and which conversion operators do I need to delete? Example on godbolt 回答1: You might do

Forbid integer conversion with precision loss

荒凉一梦 提交于 2021-02-18 06:13:21
问题 How to prevent such code from compiling? #include <vector> #include <limits> #include <iostream> #include <cstdint> int main() { std::vector<int16_t> v; v.emplace_back(std::numeric_limits<uint64_t>::max()); std::cout << v.back() << std::endl; return 0; } g++ and clang with -std=c++14 -Wall -Wextra -Werror -pedantic -Wold-style-cast -Wconversion -Wsign-conversion don't even warn about it. The example also compiles without warnings with std::vector<uint16_t> 回答1: Add -Wsystem-headers to the

Check existence of (global) function but disallow implicit conversion

随声附和 提交于 2021-02-11 17:08:12
问题 Consider this simple check for whether a (global) function is defined: template <typename T> concept has_f = requires ( const T& t ) { Function( t ); }; // later use in MyClass<T>: if constexpr ( has_f<T> ) Function( value ); unfortunately this allows for implicit conversions . This is obviously a big risk for mess-ups. Question: How to check if Function( const T& t ) 'explicitly' exists? Something like if constexpr ( std::is_same_v<decltype( Function( t ) ), void> ) should be free of implict

Check existence of (global) function but disallow implicit conversion

血红的双手。 提交于 2021-02-11 17:06:45
问题 Consider this simple check for whether a (global) function is defined: template <typename T> concept has_f = requires ( const T& t ) { Function( t ); }; // later use in MyClass<T>: if constexpr ( has_f<T> ) Function( value ); unfortunately this allows for implicit conversions . This is obviously a big risk for mess-ups. Question: How to check if Function( const T& t ) 'explicitly' exists? Something like if constexpr ( std::is_same_v<decltype( Function( t ) ), void> ) should be free of implict

The sum of n terms of fractional expression is an integer when it should be a float

╄→гoц情女王★ 提交于 2021-02-11 14:44:10
问题 In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have declared term as float, Shouldn't that be enough? #include <stdio.h> int main() { float sum = 0, term; int n, i; printf("enter the value of n:\n"); scanf("%d", &n); term = 1.0 / n; for(i = 1; i <= n; i++) { sum = term + sum; } printf("Sum = %.3f\n", sum); return 0; } 回答1: In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have

what is array decay in c and when it happen?

折月煮酒 提交于 2021-02-11 12:19:26
问题 I am currently studying C language. I wonder what 'array decaying' means, and when it happens. And I wonder if the two variables below are interpreted in the same way. char(*zippo)[2] = NULL; char zippo2[4][2]; zippo = (char(*)[2])malloc(sizeof(char[2]) * 4); 回答1: From the C Standard (6.3.2.1 Lvalues, arrays, and function designators) 3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type

Ambiguous implicit user defined conversions in .NET

这一生的挚爱 提交于 2021-02-10 20:15:08
问题 I recently wrote a couple of structs in .NET, to which I then added implicit conversion operators Example: public struct Alpha { internal string value; public static implicit operator Alpha(Beta b) { return new Alpha() { value = b.value }; } public static implicit operator Beta(Alpha a) { return new Beta() { value = a.value }; } } public struct Beta { internal string value; public static implicit operator Alpha(Beta b) { return new Alpha() { value = b.value }; } public static implicit

Why doesn't g++ -Wconversion warn about conversion of double to long int when double is constant?

ε祈祈猫儿з 提交于 2021-02-09 07:15:29
问题 If I pass a double to a function requiring long, g++ warns of conversion problem, but if I pass a const double to a function requiring long, g++ is happy. The warning is the following: warning: conversion to ‘long int’ from ‘double’ may alter its value [-Wconversion] I would like g++ to give me a warning whether I pass a double or a const double. How would I do so? I have makefile and some code you can run. I like to turn on as many warnings as I can, but perhaps one is implicitly shutting