I created a Cuda 5 project (VC++) in Visual Studio, and I want to use boost chrono API. When I include boost/chrono.hpp, I have the following error:
Error 89 error C2675: unary '-' : 'T' does not define this operator or a conversion to a
type acceptable to the predefined operator D:\StandaloneApp\Library\Vc\boost_1_52_0\boost\ratio\detail\mpl\abs.hpp
68 1 ThrustCompareSoaAndAos
The code block around line 68:
#if defined(BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2)
namespace aux {
template< typename T, T n > struct abs_wknd
{
BOOST_STATIC_CONSTANT(T, value = (n < 0 ? -n : n));
typedef integral_c<T,value> type; // line 68
};
}
#endif
What causes this error?
I changed the line:
BOOST_STATIC_CONSTANT(T, value = (n < 0 ? -n : n));
to
BOOST_STATIC_CONSTANT(T, value = (n < 0 ? (n * -1) : n));
this complied.
Related
I started learning Groovy and got curious about its method invocation resolving. I've read and understand what is groovy multi-methods, but I can't understand behaviour of next code snippet
def method(int i) {
println 1
}
def method(Number n) {
println 2
}
Number n = 1
method n # returns 1 as expected
method((Number) n) # returns 2 which I can't understand why
Is there some exclusion about casting? Can someone please explain this behaviour?
I think what you see here is a result of value in-/out-boxing.
If you pass a value of actial type Integer in, the value is converted to int during method lookup and the 1st method is called.
If you cast the value to Number or it's sub-class explicitly the inboxing does not happen and the method expecting Number is invoked.
I extended your code a bit:
def method(int i) {
println "int > $i / ${i.getClass()}"
}
def method(Number i) {
println "Number > $i / ${i.getClass()}"
}
def method(float i) {
println "float > $i / ${i.getClass()}"
}
Number n = 1
Number nFloat = 1.0f
Number nDouble = 1.0d
method n
method( new BigInteger( n ) )
method( (Number)n )
method( new BigInteger( n ) as int )
method nFloat
method nDouble
prints
int > 1 / class java.lang.Integer
Number > 1 / class java.math.BigInteger
Number > 1 / class java.lang.Integer
int > 1 / class java.lang.Integer
float > 1.0 / class java.lang.Float
Number > 1.0 / class java.lang.Double
Hope it is getting clearer now...
I've got exercise about linear equation in Pascal and I've created simple code for comparison input numbers but when I try to run it. I have problem about incompatible types, got BOOLEAN and expected LONGINT.
program LinearEquation;
var
a, b: real;
begin
readln(a, b);
if (b = 0 and a = 0) then
writeln('INFINITY')
else if (b = 0 and a <> 0) then
writeln(1)
else if (a = 0 and b <> 0) then
writeln(0)
else if(b mod a = 0) then
writeln(1);
readln;
end.
and
13 / 9 rownan~1.pas
Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
15 / 14 rownan~1.pas
Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
17 / 14 rownan~1.pas
Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
17 / 14 rownan~1.pas
Error: Incompatible types: got "BOOLEAN" expected "LONGINT"
At least in modern Delphi, and has higher precedence than =, so
a = 0 and b = 0
is interpreted as
(a = (0 and b)) = 0.
But the and operator cannot accept an integer and a floating-point value as operands (two integers would have been OK, though). Hence the error.
Had a and b been integers, 0 and b would have been the bitwise conjunction of 0 and b, that is, 0. Thus, we would have had
(a = 0) = 0.
This reads either true = 0 (if a is equal to 0) or false = 0 (if a is different from 0). But a boolean cannot be compared to an integer, so the compiler would have complained about that.
Still, this was just an academic exercise. Clearly, your intension was
(a = 0) and (b = 0).
Just add the parentheses:
if (b = 0) and (a = 0) then
writeln('INFINITY')
I am writing the following ternary condition in my code:
auto shift =
(auto diff1 = setPositions.back() - setPositions.front()) ==
(auto diff2 = posM - posN) ?
diff1 :
diff1 > diff2 ?
diff2 - (diff1 - diff2) :
diff1 + (diff2 - diff1);
The above code appears within the constructor definition. The class declaration is as given below:
typedef int32_t THIRTY_TWO_BIT_INT;
class setBits{
public:
setBits(THIRTY_TWO_BIT_INT, THIRTY_TWO_BIT_INT,
unsigned short, unsigned short
);
~setBits();
private:
THIRTY_TWO_BIT_INT valA;
THIRTY_TWO_BIT_INT valB;
unsigned short posN;
unsigned short posM;
THIRTY_TWO_BIT_INT result;
vector<int>setPositions;
void calcSetPositions(THIRTY_TWO_BIT_INT&);
THIRTY_TWO_BIT_INT bm;
void calcBitMask();
void remRedSetBits(THIRTY_TWO_BIT_INT&);
};
For the above ternary condition code, I get the following compilation error:
1 In file included from main.cpp:1:
2 ./code.hpp:42:15: error: expected ')'
3 (auto diff1 = setPositions.back() - setPositions.front()) ==
4 ^
5 ./code.hpp:42:9: note: to match this '('
6 (auto diff1 = setPositions.back() - setPositions.front()) ==
7 ^
Can anyone point out the cause of this error?
You can't declare new variables inside of the ?: operator like that. You will have to break up the code into multiple statements, eg:
auto diff1 = setPositions.back() - setPositions.front();
auto diff2 = posM - posN;
auto shift =
(diff1 == diff2) ?
diff1 :
(diff1 > diff2) ?
diff2 - (diff1 - diff2) :
diff1 + (diff2 - diff1);
I've written an expression that finds the largest of two variables and returns true if it is equal to or above a limit variable. They are all integers.
if max(num1, num2) >= limit {
result = 3
} else {
result = 2
}
this works fine, but if I try to put it in the more compact ternary format it is rejected with: 'could not find an overload for '>=' that accepts the supplied arguments'.
max(num1, num2) >= limit ? result = 3 : result = 2
I've tried putting the conditional in various bracket configurations but it still fails. Any ideas?
Further experimentation reveals that the problem is related to the limit var being set as implicitly unwrapped variable, although this is set in the init block:
limit: Int!
init (num: Int) {
limit = num
}
Many thanks.
Kw
The problem here is precedence, it should "work" if you have written
max(num1, num2) >= limit ? (result = 3) : (result = 2)
// ^ ^
Note the precedence of each operator:
?:: associativity right precedence 100 (higherThan: AssignmentPrecedence)
=: associativity right precedence 90
so a ? b : c = d will be evaluated as (a ? b : c) = d since the ternary conditional operator has higher precedence than assignment.
Here in max(num1, num2) >= limit ? result = 3 : result, the type of result = 3 is () and result is Int, so there is a type mismatch between the two arms and caused error.
(However I'm not sure why the message talks about >=, please check if your num1 and num2 are also Int.)
That said, it is very unconventional (i.e. bad style) to put an assignment inside a ?:, typically you want to write this instead:
result = max(num1, num2) >= limit ? 3 : 2
#define BIT2 (1 << 2)
#define BIT0 (1 << 0)
unsigned int a = 0, temp = 0;
#define setBit2_a (a |= BIT2)
#define clearBit2_a (a &= ~BIT2)
#define setBit0_a (a |= BIT0)
#define clearBit0_a (a &= ~BIT0)
void main()
{
a=4; //use a scanf here for convinient
temp = a;
a & BIT0 != 0 ? setBit2_a : clearBit2_a;
temp & BIT2 != 0 ? setBit0_a : clearBit0_a;
printf("the number entered is a = %u\n\r", a);
}
this should set the bit 0 in the variable a , but its not doing so in ubuntu gcc complier can anybody please explain this
Note that you probably expect a different result from your expression a & (1 << 2) != 0: the operator precedence for == is stronger than for & so the evalution results in a & ((1 << 2) != 0) which is always false for your ternary operator since 4 & 1 == 0
You want: (a & (1 << 2)) != 0 ? ...; or a & 4 ? ...;
all that is to be noted here is:
the operator precedence for == is stronger than for & so the evaluated results which is always false and we need to use the braces here in accordance with the precedence and the BODMAS rule.