C4930

错误消息
“prototype”: 未调用原型函数(是有意用变量定义的吗?)

编译器检测到未使用的函数原型。如果有意将该原型作为变量声明,则移除左/右括号。

下面的示例生成 C4930:

// C4930.cpp
// compile with: /W1
class Lock {
public:
   int i;
};

void f() {
   Lock theLock();   // C4930
   // try the following line instead
   // Lock theLock;
}

int main() {
}

当编译器无法分清函数原型声明与函数调用时,也会出现 C4930。

下面的示例生成 C4930:

// C4930b.cpp
// compile with: /EHsc /W1

class BooleanException
{
   bool _result;

public:
   BooleanException(bool result)
      : _result(result)
   {
   }

   bool GetResult() const
   {
      return _result;
   }
};

template<class T = BooleanException>
class IfFailedThrow
{
public:
   IfFailedThrow(bool result)
   {
      if (!result)
      {
         throw T(result);
      }
   }
};

class MyClass
{
public:
   bool Foo()
   {
      try
      {
         IfFailedThrow<>(MyMethod()); // C4930

         // try one of the following lines instead
         // IfFailedThrow<> ift(MyMethod());
         // IfFailedThrow<>(this->MyMethod());
         // IfFailedThrow<>((*this).MyMethod());

         return true;
      }
      catch (BooleanException e)
      {
         return e.GetResult();
      }
   }

private:
   bool MyMethod()
   {
      return true;
   }
};

int main()
{
   MyClass myClass;
   myClass.Foo();
}

在上面的示例中,不含参数的方法的结果作为参数传递给未命名本地类变量的构造函数。该调用会产生歧义:既可以是命名本地变量,也可以是使用对象实例以及相应的指向成员的指针运算符给方法调用加前缀。

posted @ 2013-01-22 19:48  avexer  阅读(205)  评论(0编辑  收藏  举报