小编5go*_*der的帖子

成对bool和c ++模板

我正在编写一个模板,它接受任意数量的参数,并在这些值上找到布尔AND.

template <bool... Vs> struct meta_bool_and;

template <bool V> struct meta_bool_and : std::integral_constant<bool, V> {}; 

template <bool V, bool... Vs> 
struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
Run Code Online (Sandbox Code Playgroud)

但是,我无法通过以下消息进行编译

 error: redeclared with 2 template parameters
 struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

c++ templates partial-specialization variadic-templates c++11

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

将内联函数转换为宏

我有一个1行inline函数,它是我的代码中的热点的一部分.我想看看将此更改为宏是否有益.写作功能我不必担心副作用.但是如何在没有副作用的情况下为此编写宏?

#define FLAG1_BIT 4
struct node
{
    unsigned long key;
    struct node* child[K];      //format <address,flag1,flag2,flag3>
};

static inline bool isFlag1Set(struct node* p)
{
    return ((uintptr_t) p & FLAG1_BIT) != 0;
}
Run Code Online (Sandbox Code Playgroud)

c macros inline-functions c-preprocessor

0
推荐指数
1
解决办法
526
查看次数

Keil ARM C编译器:内联函数的麻烦

我正在将实时内核TNeoKernel移植到Cortex-M架构,所以我安装了Keil并且正在尝试构建内核.但是,我遇到了意想不到的问题:编译器似乎无法处理inline函数.这是简单的代码:

static inline int test(void)
{
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器的输出如下:

src\appl\main.c(17): warning:  #260-D: explicit type is missing ("int" assumed)
  static inline int test(void)
src\appl\main.c(17): error:  #65: expected a ";"
  static inline int test(void)
Run Code Online (Sandbox Code Playgroud)

如果我删除inline关键字,它会编译并运行.

在ARM编译器的文档中,我找不到任何关于inline函数的信息.因此,只是为了确保:inlineARM编译器是否真的不支持该关键字?这太令人难以置信所以我决定问.

static inline在内核的独立于平台的代码中有很多函数,那么,它支持ARM编译器的最佳方法是什么?在我的头脑中,我只有两个想法:

  • 创建与体系结构相关的宏TN_INLINE,对于ARM编译器,它应该扩展为空;
  • 将非常小的1行函数转换为宏.

c arm inline-functions keil

0
推荐指数
1
解决办法
2740
查看次数

c ++ 11:为什么"auto&"没有推断出正确的类型?

我有以下代码:

struct MyType{};
using vec_type = std::vector<std::unique_ptr<MyType>>;
void foo(vec_type vec, vec_type& vec2, vec_type::iterator itr){
  for (auto &ri = vec.rbegin(); ri != vec.rend(); ++ri) {
    vec2.insert(itr, std::move(*ri));
  }    
}
Run Code Online (Sandbox Code Playgroud)

它在for循环行中收到以下错误:

non-const lvalue reference to type 'reverse_iterator<[...]>' cannot bind to a temporary of type 'reverse_iterator<[...]>'
Run Code Online (Sandbox Code Playgroud)

然后我需要更改为以下内容才能进行编译:

vector<unique_ptr<Titem> >::reverse_iterator ri = replaces.rbegin();
for (; ri != vec.rend(); ++ri) {
    vec2.insert(itr, std::move(*ri));
}    
Run Code Online (Sandbox Code Playgroud)

这对我没有意义 - 我没有看到两个代码之间有任何语义差异.编译器不应该只是推断出"自动"是vector<unique_ptr<Titem> >::reverse_iterator什么?

我正在使用clang ++ 3.5.

c++ type-inference c++11

0
推荐指数
1
解决办法
189
查看次数

C中字符数组中的字符串终止符

我正在尝试根据一组规则制定一个程序来决定密码的有效性.

这是我有的:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
  int uppercase = 0;
  int length = 0;
  int numbers = 0;
  int others = 0;
  char password[13];
  char yesOrNo;
  printf("Your password must be 8-12 characters long.\n"
         "It must contain at least one symbol, uppercase letter, and number.\n\n");
 COMEBACK:
  printf("Please enter your password:");
  scanf(" %s", &password);
  while (password != 'NULL') { // Tried 0 here, tried '\0', but to no avail.
    if (isalpha(password)) {
      length …
Run Code Online (Sandbox Code Playgroud)

c arrays terminator

-1
推荐指数
1
解决办法
2223
查看次数

程序不会返回String

所以我在编程课程的介绍中遇到了一个问题.我做了一些研究,以实现这个问题的方法,我能够编译没有错误,但其中一个规定是我必须返回一个字符串.这是我把头伸进砖墙的地方.我已经尝试了一些方法来解决这个问题,但我希望有人能在这里发现我一直把头发拉过来的问题.

public class TellSeason {

    public static void main(String[] args) {
        season(5 , 12);
    }

    public static String season(int month , int day) {

        if ((month == 9 && day >=16)
            || month == 10
            || month == 11
            || (month == 12 && day <=15)) {
            return ("Fall");
        }
        else if ((month == 12 && day >=16)
                 || month == 1
                 || month == 2
                 ||(month == 3 && day <=15)) {
            return ("Winter");
        }
        else if ((month == …
Run Code Online (Sandbox Code Playgroud)

java string return

-1
推荐指数
1
解决办法
221
查看次数

循环创建线程

像这样在循环中创建线程是否不好?

线程函数示例:

void *thread_func(void* args)
{
  const char *str = (const char *)args;

  printf("Threading %s\n", str);
  usleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

主循环:

pthread_t thread;
while (1) {
    char *str = "test str";
    if (pthread_create(&thread, NULL, thread_func, str)) {
            fprintf(stderr, "Failed to create thread\n");
            return -1;
    }
    usleep(3000);
  /* Thread guaranteed finished here */
  }
Run Code Online (Sandbox Code Playgroud)

或者我必须创建一次并循环使用

c multithreading gcc pthreads

-1
推荐指数
1
解决办法
9420
查看次数

printf不工作为什么?

没有编译器错误,但我没有看到printf语句(循环后).循环前的printf都很好.我想不出来解决这个问题.

   int prime(int n); 


    int main(void)
    {
        int num = 0; 
        bool prime; 
        int n; 
        int count; 
        int sum; 

        printf("Enter the limit:", n); 
        scanf("%d", &n);
        printf("Primes up to %d\n", n); 


        // loop 
        for(int i =2; i <= n; i++)
        {
            prime = true; 
            for(int j =2; j < i; j++)
            {
                if(i%j == 0)
                {
                    prime = false;
                    break; 
                }
            }
            if(prime)
            {
                num += i; 
                printf("%d ", i);

            }
        }
        return num;
        count++;
        sum += count; 
        // sum of …
Run Code Online (Sandbox Code Playgroud)

c

-3
推荐指数
1
解决办法
108
查看次数