Questions tagged [implicit-conversion]
Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.
2,404
questions
0
votes
2
answers
62
views
Why float to int is not implicitly converted in Java when both uses 4 bytes in memory?
Why is it so that both float and int requires 4 bytes of memory storage, but int can be converted to float and float cannot to int? Please explain in terms of memory storage, like what happens in the ...
4
votes
1
answer
113
views
C++ Implicit cast for operator==
I'm having trouble understanding why one template instantiation works in my code and another does not.
I am using this class template I created:
template <typename T>
struct Allocated {
public:
...
1
vote
1
answer
45
views
How to solve the problem incompatible pointer types passing 'char (*)[64]' to parameter of type 'const char **'
I kept getting incompatible pointer types passing 'char (*)[64]' to parameter of type 'const char **' when I pass the name[100][64] in the sort function and end up getting segmentation fault 11
#...
0
votes
0
answers
37
views
Rust &String to &str implicit conversion [duplicate]
Let's consider this code for my question
The playground link can be found here https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=400f59f484d86826de5829ba3f29fc89
fn ...
1
vote
1
answer
89
views
Do C# 10 inferred delegate types involve an implicit conversion?
Inferred delegate types have been introduced since C# 10.
However, I am quite confused with the behavior of the feature:
Generally, I think we can reasonably expect that, if this can compile:
var x = (...
-2
votes
1
answer
105
views
Why i cannot use j>=num-i in the inner loop? [closed]
When i use j>=num-i in the inner loop ,program will throw a segmentation fault.
#include <iostream>
using namespace std;
int main(){
cout << "Enter the number of rows: ";
...
2
votes
1
answer
70
views
Can multiple values be implicitly converted into an object?
Is there a way to implicitly convert multiple variables to a class object when passing them into a function? It works with a single variable, but when I try to pass multiple variables, it would not ...
3
votes
0
answers
49
views
Unitful - Automatic conversion to preferred units under multiplication and division
I've just started using Unitful.jl to automatically perform dimensional analysis and unit conversion in my code. This does exactly what I want if I add or subtract variables - I work with atoms and ...
0
votes
3
answers
83
views
Difference between pointer to pointer and 2d array
EDIT
This is not a question on array decay from char [1] and char *. I know what array decay is for 1D arrays. It however seems different for char [1][1] and char ** since they are not even compatible ...
3
votes
1
answer
83
views
Compilation error in template when const is removed
template<typename T>
void func(const T &x) {
cout << "base template "<<x<<"\n";
}
template<>
void func(const int &x) {
cout << &...
2
votes
0
answers
34
views
Are allocators required to be interconvertible?
I wondered for a long time how it is possible to assign an allocator of type A to an object expecting an allocator of type B. Check the following code:
Demo
#include <iostream>
#include <...
3
votes
2
answers
94
views
Is it possible to derive template parameters from a literal integer in a formula somehow?
I have a class that wraps an integer into a range of values known only to the compiler (and the developer), the limits are unknown at runtime. The class implements operators such that the limits ...
2
votes
0
answers
64
views
Templated conversion operator prevents operator usage of (implicit) converted-to type
I am trying to understand why the compiler is able to use the operator int operator+(const int lhs, const int rhs) with the regular conversion function in the Wrapper class, but as soon as I make it ...
3
votes
4
answers
144
views
Why can you use a function as an if-statement condition without calling it? [duplicate]
I was wondering why both versions of this code have no error:
#include <iostream>
bool check_number(int n) {
return n < 5;
}
int main() {
int number = 6;
// (1) prints "...
3
votes
3
answers
108
views
Trouble understanding char* and string in CS50
So I know that a string is just an array of characters that are stored consecutively in a computer's memory.
I also know that in order to find out the location of a string, you just have to go to the ...
-1
votes
2
answers
95
views
sizeof(pointer_to_a_function) vs sizeof (function_name)
Here is the program as text:
#include <stdio.h>
int f(int x){
return x+1;
}
int main() {
int (*p)(int) = f;
printf( "%d\n", sizeof(p) );
printf( "%d\n", ...
3
votes
2
answers
84
views
Clarification on assigning hexadecimal constant to a variable of type `signed`
I am reading Jans Gustedt's "Modern C". In a discussion about how the types for numerical literals are determined, he writes about the common pitfall of assigning 0xFFFFFFFF to a signed ...
0
votes
2
answers
59
views
C Asterisk Operator Using
im trying to learn pointers and im confused in second line, can someone explain how it works?
if we suppose 'a' base address is 100
int a[3][3] = {6, 2, 5, 0, 1, 3, 4, 9, 8};
printf("%p \n&...
4
votes
3
answers
105
views
Is passing pointer to an array as pointer to pointer UB in C?
I have such a code:
#include <stdlib.h>
#include <stdio.h>
void func(int **b)
{
printf("b = %p\n", b); // 0x7ffe76932330
*b = *b + 1;
}
int main(void)
{
int b[10] = ...
1
vote
2
answers
122
views
Is there any difference between bool foo = true and bool foo = 1?
In C++, is there any difference between assigning a bool to true and assigning it to 1(or any non-zero integer)?
I have seen many competitive programmers use 1 instead of true even when the code is ...
-2
votes
1
answer
42
views
Query about Implicit Type casting in Java for Primitive data types
I HAVE BEEN LEARNING ABOUT TYPE CASTING IN JAVA AND THIS HAPPENED.
First I gave a large value to an integer variable(larger number than the range of byte data_type).
Next I created a byte variable and ...
3
votes
2
answers
138
views
Why is int to float conversion failing in printf?
When using a int to float implicit conversion, it fails with printf()
#include <stdio.h>
int main(int argc, char **argv) {
float s = 10.0;
printf("%f %f %f %f\n", s, 0, s, 0); ...
0
votes
0
answers
39
views
Idk where I supposedly declared my function as a different type (C) [duplicate]
Well I got this issue and I don't get why? If someone could explain I'm just trying to learn C lmao
I don't even have a clue of where I'm supposed to let it implicitly declare this function as ...
4
votes
1
answer
112
views
Why implicit conversion doesn't work when inserting to map
I have a structure that is implicitly convertable to the type that is acceptable by the std::map::insert() function.
I use a structure as-is, but I have compilation error:
no overloaded function ...
0
votes
0
answers
34
views
Why doesn't the compiler understand to implicitly cast a lambda expression?
Considering the following sample
public struct EnumSafeExpression<TFunc>
{
public Expression<TFunc> SafeExpression { get; }
/// <summary>
/// If we ...
0
votes
2
answers
100
views
How can I implicitly convert an enum to its subset and vice versa in C++?
More precisely, the feature I want is like implicitly convert an enum to its subset enum and vice versa.
The code I wish it working:
enum class Human {
A = 1,
B = 2,
};
enum class Male { //...
3
votes
2
answers
81
views
Can someone give examples of a user-defined conversion sandwiched between two standard conversion sequences in C++? [closed]
The C++ spec, allows:
Implicit conversion sequence consists of the following, in this order:
zero or one standard conversion sequence;
zero or one user-defined conversion;
zero or one standard ...
5
votes
1
answer
137
views
Why is the compiler unable to match the types automatically on `size_t` variables in a ranged base for loop?
I stumbled upon a problem with ambiguous overload for operator<< when using std::views::enumerate with size_t range.
More specifically using this code:
#include <iostream>
#include <...
1
vote
2
answers
52
views
The ternary operator overhead of ITC for all of its operands
I cannot help myself but struggle to understand why is the ternary conditional's 3rd operand (the false-condition expression) subject to implicit type conversion even when the control condition ...
1
vote
1
answer
90
views
C# Object List Type Change based on condition
I am developing a complex application on .NET 6 using Microsoft Blazor. There are many classes and many functions manipulating List<Object> with different way, but also there are cases where the ...
1
vote
2
answers
63
views
Behavior of C bug: passing pointer to array instead of array into function
I recently came across a bug in my C firmware, in which I passed the wrong type into the function. It raises a warning, but I want to understand what could happen in this scenario. During debugging, ...
1
vote
1
answer
74
views
Kotlin - Spring - Endpoint Enum conversion
I have this :
@RestController("/test")
class MyController(
private val myService: MyService
) {
@RequestMapping(value = ["/test"], method = [ RequestMethod.POST, ...
0
votes
1
answer
53
views
Automatic conversion of contiguous container type of contigous container types into a another generic "contiguous container type view"
I have the following code godbolt link:
#include <iostream>
#include <array>
#include <vector>
#include <span>
#include <cstdint>
#include <string>
template <...
0
votes
2
answers
73
views
Program hangs after modifying 2D array in a function in C [duplicate]
The program is a Tic-Tac-Toe game, and it hangs after taking the X and Y values from the user.
This is the main file:
#include <stdio.h>
#include <string.h>
#include "input.cpp"
...
2
votes
1
answer
60
views
`const_cast`, top-level const and lvalue-to-rvalue conversions
Let's assume we have a simple piece of code,
int main() {
int x = 5;
int* const y = &x;
const_cast<int*>(y);
}
Is my assumption that a lvalue-to-rvalue conversion happens inside the ...
0
votes
2
answers
30
views
Convert back from ILS currency to a number
I have a Currency input that is formatted to ILS (israeli new Shekel) and I want my onChange event to record the value, but as a number. this is what I tried:
<CurrencyInput
...
0
votes
2
answers
67
views
Array decays to pointer as function parameter
I know following function definitions are considered the same by c compiler
void test(int* array); // all converted to first one
void test(int array[]);
void test(int array[3]);
and in C99 standard
...
2
votes
2
answers
84
views
What is the size of 'ptr'? [closed]
#include<stdio.h>
int main()
{
char *s[] = {"ice","green","cone","please"};
char **ptr[] = {s+3,s+2,s+1,s};
char ***p = ptr;
printf("%...
0
votes
1
answer
51
views
How to write constructors for implicit conversions from instances of a templated class C<Derived> to instances of C<Base>
I have created a template class derived from shared_ptr<const T> that enables a holder of the shared pointer to modify the pointed instance by cloning the instance and reseting the ...
0
votes
1
answer
43
views
Implicit conversion differencies
The following works:
case class Fraction(d: Int, n: Int) {
def *(other: Fraction) = Fraction(d * other.d, n * other.n)
}
implicit val int2fraction: Int => Fraction = { x => Fraction(x, 1) }
...
0
votes
2
answers
270
views
What is the difference between implicit and explicit casting in Java?
What is the easiest way to include both implicit and explicit casting to your code? It is a requirement for my Java project.
Graphic g = getGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new ...
1
vote
2
answers
114
views
Where is the implicit conversion that changes signedness?
I am having trouble with the following code:
#include <stdio.h>
#include <stdlib.h>
void someFunction(int number);
int main()
{
printf("Hello world!\n");
return 0;
}
...
0
votes
1
answer
69
views
Why can a function receive a char array as a char pointer and why can the function change the value of an element?
I declare char pointer as a parameter of a function and put a char array as an argument.
I was told that char pointer and char array are different types.
But the function can have a char array as a ...
0
votes
2
answers
90
views
Error expected unqualified-id before '=' token when assigning a function pointer to the address of another function
I have the following program that I attempt to use function pointer to "save memory",by saving an address of a function to a member variable, which is the function pointer.
The function ...
-3
votes
1
answer
145
views
Is this conversion valid in C
As direct conversion to void ** of other types is not valid is this conversion valid in C?
void swapValues(void **av, void **bv, size_t size)
{
int *a = *av;
int *b = *bv;
char array[...
1
vote
2
answers
79
views
Swapping Array elements call by reference
I had to swap array elements with call by reference to reduce the lines of code in my bigger project, and I am successful in swapping them but this code gives a bunch of warnings, how do I do this ...
0
votes
2
answers
216
views
no suitable conversion function from "const std::filesystem::path" to "char *" exists
I'm a C++ beginner. I've been learning C++ for a week now.
I was messing around with the <filesystem> library, trying to learn new things. I tried to make a function to receive a path to a .txt ...
1
vote
2
answers
73
views
Why is pointer decay okay but externally linking an array as a pointer not okay? [duplicate]
Given that C insists on decaying array types into pointers, when passing arrays to functions, then why is it UB to externally link to an array as a pointer?
As an example:
MyFile.c
// Declare and ...
1
vote
2
answers
54
views
How does this C code work? The ptr assignment is unclear
#include <stdio.h>
int main()
{
char a[]={1,2,3,4,5,6,7};
char *ptr =(char*)(&a+1);
printf("%d %d \n",*(a+1),*(ptr-1));
return 0;
}
The output is: 2 7
I'm unable to ...
2
votes
3
answers
70
views
Why is declaring a function parameter instead of function pointer parameter allowed?
I don't really know what I expect as an answer to this but being able to declare function parameter while not being able to declare variable inside a function the same way makes me uneasy.
#include &...