我正在尝试编写一个简单的无状态sesssion bean,但是我在查找时给出了窄参考的问题.我有
阶级演绎
我用
eclipse IDE
我的豆类
package codes;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class SinaBean implements SessionBean {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getHello()
{
return "hello";
}
public void ejbCreate(){
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
@Override
public void ejbRemove() throws EJBException, RemoteException { …Run Code Online (Sandbox Code Playgroud) 自从用g ++打开C++ 0x标准后,我开始看到'缩小转换'错误,特别是在从'int'转换为'short'时,尽管我理解错误涵盖了更广泛的转换.
任何人都可以对引入这种额外安全级别的理性有所了解吗?禁用此错误会产生什么后果?(除了潜在的精度损失).
谢谢.
使用{}初始化程序C++11初始化会bool b = {2}产生以下警告消息:
warning: narrowing conversion of ‘2’ from ‘int’ to ‘bool’ inside { } [-Wnarrowing]
Run Code Online (Sandbox Code Playgroud)
但是,使用旧样式bool b = 2没有这样的问题.这背后的原因是什么?
更新:我使用编译代码g++ -std=c++11,它给了我警告.如果我添加该选项-pedantic-errors,警告将成为错误.
使用编译此代码g++ -std=c++17 -Wall -pedantic main.cpp不会产生任何警告:
#include <iostream>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
for (int i = 0; i < 100; ++i) {
float x = 300.0 + rand();
char c = x;
std::cout << c << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不应该产生缩小误差吗?
考虑以下程序:
template<typename T>
constexpr int f()
{
T{}.i; // error if instantiated with [T = double]
return 42;
}
constexpr void g(char);
using U = decltype( g( {f<double>()} ) );
Run Code Online (Sandbox Code Playgroud)
根据我的理解,最后一行是一个错误,因为调用f<double>()是在大括号初始值设定项中,即使f<T>返回int,int也需要返回的值来决定是否可以char按预期将其缩小为 a g。这需要使用f来实例化的定义double,这会导致错误。gcc 和 clang 都拒绝此代码。
但是,如果将 的定义g更改为接受int参数:
constexpr void g(int);
Run Code Online (Sandbox Code Playgroud)
那么似乎没有必要实例化 的定义f,因为缩小转换必须成功。事实上,GCC接受这一点,但仍然铿锵实例f与double和拒绝代码。此外,如果f仅声明,但未定义,clang 接受代码,这意味着不需要定义,不应实例化。
我的推理是否正确,这是一个clang错误,还是需要实例化,这实际上是一个gcc错误?
为什么 Typescript 编译器会抱怨以下代码?
type Foo = {
a: string
}
type Bar = {
b: number
}
type Baz = Foo | Bar;
function f(x: Baz): number {
if (x.a) { // property 'a' does not exist on type Bar!
return 0;
}
if (x.b) { // property 'b' does not exist on type Foo!
return 1;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
下列
int i = 0;
double d{i};
Run Code Online (Sandbox Code Playgroud)
给出 a 的错误(在 clang 中)或警告(在 gcc 中)narrowing conversion from 'int' to 'double'。我发现令人惊奇的是,这确实在缩小,至少在我看到从 unsigned 到 double 的缩小转换之前是这样。
我的实际问题源于一个包含数组并提供一个构造函数来以最简单的方式(转发)指定数组元素的类:
template<typename T, size_t N>
struct A
{
T a[N];
template<typename... E>
A(E&&... e) : a{std::forward<E>(e)...} { }
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们有以下内容(实例):
int i = 0;
A<double, 2> x(i, 2); // ERROR for both 'i' and '2'
double y[2]{i, 2}; // ERROR for 'i' only
Run Code Online (Sandbox Code Playgroud)
其中ERROR指的是如上所述的缩小转换。我怀疑所有这些错误都归结为开头提到的错误(double d{i};)。是这样吗?否则,会发生什么? …
如果我使用 clang 工具,建议使用 clang 或 clang 工具链的某些部分来告诉我,例如将 an 传递int给需要 a 的函数short可能是一个坏主意?
鉴于这个非常简单的程序
static short sus = 0;
void foo(short us) {
sus = us;
}
int main() {
int i = 500000;
foo(i); // any indication from clang this might be a bad idea
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我一定错过了一些非常简单的东西,对吧?
如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小if子句中的类型范围typeof。但这不起作用。
例如,在if下面的子句中, 的类型event应推断为UserTextEvent, 的类型event.target应推断为HTMLInputElement:
type UserTextEvent = { value: string, target: HTMLInputElement };
type UserMouseEvent = { value: [number, number], target: HTMLElement };
type UserEvent = UserTextEvent | UserMouseEvent
function handle(event: UserEvent) {
if (typeof event.value === 'string') {
event.value // string, as expected
event.target // should be narrowed to HTMLInputElement, but
// is still HTMLInputElement | HTMLElement. Why?
}
}
Run Code Online (Sandbox Code Playgroud) type-inference discriminated-union narrowing typescript union-types
我正在尝试使用创建自定义类型保护,instanceof但奇怪的是它在 else 子句中没有按预期工作
这是带有相关 Playground 链接的示例: Playground Link
class Person {}
class Animal {}
const isPerson = (obj: Person | Animal): obj is Person => obj instanceof Person;
const isAnimal = (obj: Person | Animal): obj is Animal => obj instanceof Animal;
const test: Person | Animal = new Person();
if(test instanceof Animal){
test; // const test: Animal
}
else {
test; // const test: Person
}
if(isAnimal(test)){
test; // const test: Animal
}
else {
test; // …Run Code Online (Sandbox Code Playgroud) narrowing ×10
c++ ×6
typescript ×3
c++11 ×2
arrays ×1
c++20 ×1
clang++ ×1
eclipse ×1
ejb ×1
g++ ×1
gcc ×1
java ×1
jndi ×1
typeguards ×1
union-types ×1