Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从bool中检测cast_C++_Visual Studio 2008 - Fatal编程技术网

C++ 如何从bool中检测cast

C++ 如何从bool中检测cast,c++,visual-studio-2008,C++,Visual Studio 2008,我发现我的代码中有错误,我认为应该用警告来标记。使用/W4编译,但它不显示任何警告(仅关于未引用的形式参数) #包括 无效A(整数项、未签名整数计数、未签名团队计数) { printf(“A,count,team count\n”); } 无效A(整数项,无符号整数计数=1,布尔值为\u team=true) { printf(“A,count,is_team\n”); 返回A(项目,计数,是否团队?计数:0); } int main() { (0,false);//这里是一个例子,说明如何实现

我发现我的代码中有错误,我认为应该用警告来标记。使用/W4编译,但它不显示任何警告(仅关于未引用的形式参数)

#包括
无效A(整数项、未签名整数计数、未签名团队计数)
{
printf(“A,count,team count\n”);
}
无效A(整数项,无符号整数计数=1,布尔值为\u team=true)
{
printf(“A,count,is_team\n”);
返回A(项目,计数,是否团队?计数:0);
}
int main()
{

(0,false);//这里是一个例子,说明如何实现您的目标

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( bool ) = delete;

int main() 
{
    f( true );

    return 0;
}
应用到代码中,示例如下所示

#include <iostream>

void A( int item, unsigned int count, unsigned team_count )
{
    std::cout << "A, count, team count" << std::endl;
}

void A( int item, unsigned int count = 1, bool is_team = true )
{
    std::cout << "A, count, is_team" << std::endl;
    return A( item, count, is_team ? count : 0 );
}

void A( int, bool ) = delete;

int main() 
{
    A( 0, false );

    return 0;
}

下面是一个例子,你可以如何实现你想要的

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( bool ) = delete;

int main() 
{
    f( true );

    return 0;
}
应用到代码中,示例如下所示

#include <iostream>

void A( int item, unsigned int count, unsigned team_count )
{
    std::cout << "A, count, team count" << std::endl;
}

void A( int item, unsigned int count = 1, bool is_team = true )
{
    std::cout << "A, count, is_team" << std::endl;
    return A( item, count, is_team ? count : 0 );
}

void A( int, bool ) = delete;

int main() 
{
    A( 0, false );

    return 0;
}

下面是一个例子,你可以如何实现你想要的

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( bool ) = delete;

int main() 
{
    f( true );

    return 0;
}
应用到代码中,示例如下所示

#include <iostream>

void A( int item, unsigned int count, unsigned team_count )
{
    std::cout << "A, count, team count" << std::endl;
}

void A( int item, unsigned int count = 1, bool is_team = true )
{
    std::cout << "A, count, is_team" << std::endl;
    return A( item, count, is_team ? count : 0 );
}

void A( int, bool ) = delete;

int main() 
{
    A( 0, false );

    return 0;
}

下面是一个例子,你可以如何实现你想要的

#include <iostream>

void f( int ) { std::cout << "f( int )" << std::endl; }
void f( bool ) = delete;

int main() 
{
    f( true );

    return 0;
}
应用到代码中,示例如下所示

#include <iostream>

void A( int item, unsigned int count, unsigned team_count )
{
    std::cout << "A, count, team count" << std::endl;
}

void A( int item, unsigned int count = 1, bool is_team = true )
{
    std::cout << "A, count, is_team" << std::endl;
    return A( item, count, is_team ? count : 0 );
}

void A( int, bool ) = delete;

int main() 
{
    A( 0, false );

    return 0;
}

标准规定,这是可接受的§4.7/p4积分转换:

如果源类型为bool,则值false转换为零,值true转换为一

关于如何检测这一点(因为这不是一个错误),并且取决于您的用例,您可以自己做一些工作,或者在以下行中编写一个具有模板推断魔力的包装器:

#include <cstdio>
#include <type_traits>

template<typename T, typename U>
void A(int item, T, U) {
    static_assert(!std::is_same<T, unsigned int>::value || 
                  (!std::is_same<U, unsigned int>::value && 
                   !std::is_same<U, bool>::value), 
                  "Something was wrong");
}

template<>
void A(int item, unsigned int count, unsigned int team_count)
{
    printf("A, count, team count\n");
}

template<unsigned int count = 1, bool is_team = true>
void A(int item, unsigned int, bool)
{
    printf("A, count, is_team\n");
    return A(item, count, is_team ? count : 0);
}

int main()
{
    // A(0, false); - not acceptable
    // A(0, 22); - not acceptable
    A(0, static_cast<unsigned int>(2), false);
    A(0, static_cast<unsigned int>(33), static_cast<unsigned int>(45));
    return 0;
}
#包括
#包括
模板
无效A(整数项,T,U){
静态断言(!std::is_same::value |
(!std::is_same::value&&
!std::值是否相同,
“出了点问题”);
}
模板
无效A(整数项、未签名整数计数、未签名整数团队计数)
{
printf(“A,count,team count\n”);
}
模板
无效A(整数项、无符号整数、布尔)
{
printf(“A,count,is_team\n”);
返回A(项目,计数,是否团队?计数:0);
}
int main()
{
//A(0,false);-不可接受
//A(0,22);-不可接受
A(0,静态施法(2),假);
A(0,静态施法(33),静态施法(45));
返回0;
}


请注意,基本模板推导机制不需要C++11,尽管上面使用的一些函数需要。

标准规定这是可以接受的§4.7/p4积分转换:

如果源类型为bool,则值false转换为零,值true转换为一

关于如何检测这一点(因为这不是一个错误),并且取决于您的用例,您可以自己做一些工作,或者在以下行中编写一个具有模板推断魔力的包装器:

#include <cstdio>
#include <type_traits>

template<typename T, typename U>
void A(int item, T, U) {
    static_assert(!std::is_same<T, unsigned int>::value || 
                  (!std::is_same<U, unsigned int>::value && 
                   !std::is_same<U, bool>::value), 
                  "Something was wrong");
}

template<>
void A(int item, unsigned int count, unsigned int team_count)
{
    printf("A, count, team count\n");
}

template<unsigned int count = 1, bool is_team = true>
void A(int item, unsigned int, bool)
{
    printf("A, count, is_team\n");
    return A(item, count, is_team ? count : 0);
}

int main()
{
    // A(0, false); - not acceptable
    // A(0, 22); - not acceptable
    A(0, static_cast<unsigned int>(2), false);
    A(0, static_cast<unsigned int>(33), static_cast<unsigned int>(45));
    return 0;
}
#包括
#包括
模板
无效A(整数项,T,U){
静态断言(!std::is_same::value |
(!std::is_same::value&&
!std::值是否相同,
“出了点问题”);
}
模板
无效A(整数项、未签名整数计数、未签名整数团队计数)
{
printf(“A,count,team count\n”);
}
模板
无效A(整数项、无符号整数、布尔)
{
printf(“A,count,is_team\n”);
返回A(项目,计数,是否团队?计数:0);
}
int main()
{
//A(0,false);-不可接受
//A(0,22);-不可接受
A(0,静态施法(2),假);
A(0,静态施法(33),静态施法(45));
返回0;
}


请注意,基本模板推导机制不需要C++11,尽管上面使用的一些函数需要。

标准规定这是可以接受的§4.7/p4积分转换:

如果源类型为bool,则值false转换为零,值true转换为一

关于如何检测这一点(因为这不是一个错误),并且取决于您的用例,您可以自己做一些工作,或者在以下行中编写一个具有模板推断魔力的包装器:

#include <cstdio>
#include <type_traits>

template<typename T, typename U>
void A(int item, T, U) {
    static_assert(!std::is_same<T, unsigned int>::value || 
                  (!std::is_same<U, unsigned int>::value && 
                   !std::is_same<U, bool>::value), 
                  "Something was wrong");
}

template<>
void A(int item, unsigned int count, unsigned int team_count)
{
    printf("A, count, team count\n");
}

template<unsigned int count = 1, bool is_team = true>
void A(int item, unsigned int, bool)
{
    printf("A, count, is_team\n");
    return A(item, count, is_team ? count : 0);
}

int main()
{
    // A(0, false); - not acceptable
    // A(0, 22); - not acceptable
    A(0, static_cast<unsigned int>(2), false);
    A(0, static_cast<unsigned int>(33), static_cast<unsigned int>(45));
    return 0;
}
#包括
#包括
模板
无效A(整数项,T,U){
静态断言(!std::is_same::value |
(!std::is_same::value&&
!std::值是否相同,
“出了点问题”);
}
模板
无效A(整数项、未签名整数计数、未签名整数团队计数)
{
printf(“A,count,team count\n”);
}
模板
无效A(整数项、无符号整数、布尔)
{
printf(“A,count,is_team\n”);
返回A(项目,计数,是否团队?计数:0);
}
int main()
{
//A(0,false);-不可接受
//A(0,22);-不可接受
A(0,静态施法(2),假);
A(0,静态施法(33),静态施法(45));
返回0;
}


请注意,基本模板推导机制不需要C++11,尽管上面使用的一些函数需要。

标准规定这是可以接受的§4.7/p4积分转换:

如果源类型为bool,则值false转换为零,值true转换为一

关于如何检测这一点(因为这不是一个错误),并且取决于您的用例,您可以自己做一些工作,或者在以下行中编写一个具有模板推断魔力的包装器:

#include <cstdio>
#include <type_traits>

template<typename T, typename U>
void A(int item, T, U) {
    static_assert(!std::is_same<T, unsigned int>::value || 
                  (!std::is_same<U, unsigned int>::value && 
                   !std::is_same<U, bool>::value), 
                  "Something was wrong");
}

template<>
void A(int item, unsigned int count, unsigned int team_count)
{
    printf("A, count, team count\n");
}

template<unsigned int count = 1, bool is_team = true>
void A(int item, unsigned int, bool)
{
    printf("A, count, is_team\n");
    return A(item, count, is_team ? count : 0);
}

int main()
{
    // A(0, false); - not acceptable
    // A(0, 22); - not acceptable
    A(0, static_cast<unsigned int>(2), false);
    A(0, static_cast<unsigned int>(33), static_cast<unsigned int>(45));
    return 0;
}
#包括
#包括
模板
无效A(整数项,T,U){
静态断言(!std::is_same::value |
(!std::is_same::value&&
!std::值是否相同,
“出了点问题”);
}
模板
无效A(整数项、未签名整数计数、未签名整数团队计数)
{
printf(“A,count,team count\n”);
}
模板
无效A(整数项、无符号整数、布尔)
{
printf(“A,count,is_team\n”);
返回A(项目,计数,是否团队?计数:0);
}
int main()
{
//A(0,false);-不可接受
//A(0,22);-不可接受
A(0,静态施法(2),假);
A(0,静态施法(33),静态施法(45));
返回0;
}


请注意,基本模板推断机制不需要C++11,尽管上面使用的一些函数需要。

防止这种情况的一种非常安全的方法是将计数类型封装在类中,而不实现自动转换

class Count
{
private:
    int m_count;
public:
    explicit Count(int count) : m_count(count)
    {
        assert(m_count >= 0);
    }

    int Get() const { return m_count; }
};

class TeamCount
{
private:
    int m_count;
public:
    explicit TeamCount(int count) : m_count(count)
    {
        assert(m_count >= 0);
    }

    int Get() const { return m_count; }
};

// ...

void A(int item, Count count, TeamCount team_count);
void A(int item, Count count = Count(1), bool is_team = true);

// ...

A(0, false); // <- compiler error
A(0, TeamCount(0), false); // <- compiler error
A(0, Count(0), false); // OK
类计数
{
私人:
国际货币单位计数;
公众:
显式计数(整数计数):m_计数(计数)
{
断言(m_计数>=0);
}
int Get()常量{return m_count;}
};
班级人数
{
普里夫