I have following C++11 code using templates:
struct Base{
using count_type = unsigned short;
};
template<class T>
struct A : public Base{
using count_type = A::count_type;
// not works even if I comment this
count_type add(T x){
count_type sum = 5;
sum += x.value();
//sum += (count_type) x.value(); // not works even if I cast this, e.g.
return sum;
}
};
struct B{
using count_type = A<B>::count_type;
count_type value(){
return 5; // not works even if I do:
//return (count_type) 5;
//return 5U;
}
};
int main(int argc, char *argv[]){
B b;
A<B> a;
a.add(b);
}
When I try to compile with -Wconversion
, I get strange error message:
$ g++ -std=c++11 -Wconversion x.cc
x.cc: In instantiation of ‘A<T>::count_type A<T>::add(T) [with T = B; A<T>::count_type = short unsigned int]’:
x.cc:29:9: required from here
x.cc:11:7: warning: conversion to ‘A<B>::count_type {aka short unsigned int}’ from ‘int’ may alter its value [-Wconversion]
sum += x.value();
^
Why this happens? There is no int
anywhere?
I will be grateful if someone edit my question.
Note: clang
does not give such warning.