标签: narrowing

ejb中狭义的jndi reffrence中的类强制转换异常

我正在尝试编写一个简单的无状态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)

java eclipse ejb jndi narrowing

7
推荐指数
1
解决办法
1万
查看次数

忽略C++ 0x中缩小转换的后果是什么?

自从用g ++打开C++ 0x标准后,我开始看到'缩小转换'错误,特别是在从'int'转换为'short'时,尽管我理解错误涵盖了更广泛的转换.

任何人都可以对引入这种额外安全级别的理性有所了解吗?禁用此错误会产生什么后果?(除了潜在的精度损失).

谢谢.

c++ g++ narrowing c++11

7
推荐指数
1
解决办法
5104
查看次数

为什么`bool b = 2`运行良好,但是`bool b = {2}`会产生一个缩小转换的警告?

使用{}初始化程序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,警告将成为错误.

c++ type-conversion narrowing c++11

7
推荐指数
2
解决办法
507
查看次数

为什么在将float转换为char时C++没有显示缩小的转换错误?

使用编译此代码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)

它不应该产生缩小误差吗?

c++ gcc narrowing

7
推荐指数
1
解决办法
179
查看次数

当不需要检查缩小时,是否需要实例化函数定义?

考虑以下程序:

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>返回intint也需要返回的值来决定是否可以char按预期将其缩小为 a g。这需要使用f来实例化的定义double,这会导致错误。gcc 和 clang 都拒绝此代码。


但是,如果将 的定义g更改为接受int参数:

constexpr void g(int);
Run Code Online (Sandbox Code Playgroud)

那么似乎没有必要实例化 的定义f,因为缩小转换必须成功。事实上,GCC接受这一点,但仍然铿锵实例fdouble和拒绝代码。此外,如果f仅声明,但未定义,clang 接受代码,这意味着不需要定义,不应实例化。

我的推理是否正确,这是一个clang错误,还是需要实例化,这实际上是一个gcc错误?

c++ language-lawyer narrowing c++20

7
推荐指数
1
解决办法
126
查看次数

为什么 Typescript 无法弄清楚我的代码中的类型?

为什么 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)

链接到游乐场

narrowing typescript

7
推荐指数
1
解决办法
89
查看次数

从“int”到“double”的缩小转换和数组初始化

下列

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};)。是这样吗?否则,会发生什么? …

c++ arrays type-conversion initializer-list narrowing

6
推荐指数
0
解决办法
2171
查看次数

如何让 clang 发出关于非常简单的缩小的警告

如果我使用 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)
  • 我尝试过 -Wall 和 -Wextra,
  • 我尝试过使用 cppcoreguidelines-narrowing-conversions 进行 clang-tidy
  • 我尝试过 clang -analyze

我一定错过了一些非常简单的东西,对吧?

c++ compiler-warnings narrowing clang++

6
推荐指数
1
解决办法
696
查看次数

当属性的类型是判别式时,为什么“typeof”不缩小联合类型?

如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小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

6
推荐指数
1
解决办法
650
查看次数

如何在打字稿中的自定义类型保护中使用instanceof?

我正在尝试使用创建自定义类型保护,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 typescript typeguards

6
推荐指数
1
解决办法
201
查看次数